vector::clear Method Causing Errors

Mar 29, 2010 at 10:17pm
1
2
3
4
5
6
#define PrimaryContainer std::vector
// ...
for ( PrimaryContainer<ObjectBase*>::iterator iter = ObjStack.begin ( ); iter != ObjStack.end ( ); iter++ ) {
	delete (*iter);
}
ObjStack.clear ( );

The above code is causing the following error:

Unhandled exception at 0x00bb374a in A Shapians Tale.exe: 0xC0000005: Access violation reading location 0xfeeefef6.

Which then points to a line in the _Orphan_range method of the vector class (line 1274 in my file, using VC++ 2008). I have no idea what's going on here. I look at the call stack and it shows the clear method being the last function called by myself. The objects derived from the ObjectBase class often have iterators to the ObjStack object. Could the deletion of these iterators be causing problems?
Last edited on Mar 29, 2010 at 10:18pm
Mar 30, 2010 at 9:48am
Personally as a first action, I would comment out the delete (*iter);.
If the code runs without crashing, then we can say that the deletion is causing problems.
I would then look at the destructor function of ObjectBase to see what that is doing.


You could post the code.
Mar 30, 2010 at 5:04pm
You aren't calling erase in the loop somewhere are you?
Mar 30, 2010 at 6:59pm
The objects derived from the ObjectBase class often have iterators to the ObjStack object. Could the deletion of these iterators be causing problems?


That's confusing. Sure that could be part of it. Who knows until we see a complete example that demonstrates the problem.
Mar 30, 2010 at 7:16pm
Also note that you'll need a virtual destructor to delete the derived objects in their entirety.
Topic archived. No new replies allowed.