Re: problem: returned iterator is valid but the std:list is empty

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Fri, 9 Jun 2006 09:56:01 -0700, Jaime Stuardo
<JaimeStuardo@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Hi all...

I have a strange problem...

I have this code in a destructor :

for( std::list<COraDatabase *>::iterator it = m_listDB.begin(); it !=
m_listDB.end(); it++ )
delete (*it);

Where m_listDB is of std::list<COraDatabase *> type. Before the "for",
m_listDB.size( ) returns 0. When I debug the "for", m_listDB.begin( ) retuns
an iterator that is different from m_lisDB.end( ), so execution continues
inside the "for". Of course, application crashes, because (*it) is undefined.

Do you know what is happening here? what else can I test in order to find
the explanation and the solution?

VC's std::list maintains its size as a member variable (providing O(1) size
determination which is _not_ guaranteed), so I'd say you have corrupted
your std::list object at some point, or you've somehow triggered a bug in
the compiler. What do you expect the list size to be at this point?

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages

  • Re: Access violation at the end of the program
    ... Not, while the AV itself isn't a strange thig, ... "return" the execution jumps to some strange locatin, ... If the problem disappears then uncomment them one by one ... Can fclosebe considered a destructor? ...
    (microsoft.public.vc.language)
  • Re: Are python objects thread-safe?
    ... If you do strange things in a destructor then you can ... but I can't remember where for Python 2.x. ... It is merely using an iterator that is ... though I might have missed acquiring the ...
    (comp.lang.python)
  • Re: iterator destructors
    ... the destructor of the iterator I used in a loop. ... you "have" to pay for using the stl algorithms? ... What does the STLport ...
    (microsoft.public.vc.stl)
  • Re: Access violation at the end of the program
    ... Not, while the AV itself isn't a strange thig, ... "return" the execution jumps to some strange locatin, ... If the problem disappears then uncomment them one by ... the problem persists then you may simply be corrupting something elsewhere ...
    (microsoft.public.vc.language)
  • Re: Polymorphic behavior without virtual functions
    ... > But how is the proper destructor execution sequence occurring given ... mechanism for handling temporaries. ... The only effect of the const reference is to defer the ...
    (comp.lang.cpp)