The program crashes while it should not (VS2010)



  • My big project crashed, and I spend almost a week debugging it.

    I was able to identify bug, and I think it is in the standard library. Here is a small code to reproduce:

    #include <vector>
    
    int main(int argc, char **argv)
    {
    	std::vector<int> v(1);
    	std::vector<int>::iterator badIter;
    	std::vector<int>::iterator goodIter( v.begin() );
    	goodIter = badIter; //Crash here!
    	return 0;
    }
    

    For unknown reasons it dereferences invalid iterator inside the standard library, even if I only need to assign this invalid iterator to other variable (I will not use it!)

    Is such behaviour standard-compliant?



  • badIter isn't initialized, so it's undefinied what happens when you use it. Assigning it shouldn't crash, but maybe you have a library version which checks the iterators there.

    (btw, how does it look like when it "crash's"?)



  • In the debug library version iterator contains pointer to element and pointer to container. Both are zero in uninitialized iterator. On assignment library tries to dereference container pointer, producing "Access violation reading location 0x00000000".

    Now imagine big project which crashes in Debug mode, but works in Release. Obviously, everyone will think that program contains bug that should be fixed before compiling it in Release.



  • SAn schrieb:

    In the debug library version iterator contains pointer to element and pointer to container. Both are zero in uninitialized iterator. On assignment library tries to dereference container pointer, producing "Access violation reading location 0x00000000".

    Now imagine big project which crashes in Debug mode, but works in Release. Obviously, everyone will think that program contains bug that should be fixed before compiling it in Release.

    Hey SAn,

    it seems that this is related to the "Debug Iterator" Feature.



  • It crashes in Debug mode even if checked iterators are disabled.


  • Mod

    SAn schrieb:

    It crashes in Debug mode even if checked iterators are disabled.

    Checked iterators and debug iterators are distinct features. Use

    #define _HAS_ITERATOR_DEBUGGING 0
    

    to disable the debug iterators.



  • Thank you, SeppJ! #define _HAS_ITERATOR_DEBUGGING 0 helped.


Anmelden zum Antworten