Destructing a Vector Object causes: Free Heap block ### modified at ### after it was freed

I'm trying to destruct a vector object within my program...When the destructor is called (during "program exit" operations performed by the CPU), the following error is thrown:

HEAP[Game Engine.exe]: HEAP: Free Heap block 3b9bd0 modified at 3b9bf8 after it was freed
Windows has triggered a breakpoint in Game Engine.exe.

This may be due to a corruption of the heap, which indicates a bug in Game Engine.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while Game Engine.exe has focus.

I understand that the error is thrown because the program tried to access the vector(a vector of pointers to class objects that have already been destroyed). I tried to get around the problem by emptying the vector before its destruction...perhaps I've done it incorrectly, but during debug, the vector appears to be empty(no data)...here is vector object declaration:


1
2
std::vector<CCharacter*>gObjCharacter;
... 


and here is how I've destroyed vector object:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
CObject::~CObject(void)
{
	enabled = false;
	UINT size_t = gObjCharacter.size();	
	if(size_t)
		gObjCharacter.erase(gObjCharacter.begin(),gObjCharacter.end());
	
	size_t = gObjEntity.size();
	if(size_t)
		gObjEntity.erase(gObjEntity.begin(),gObjEntity.end());
	
	size_t = gObjModel.size();
	if(size_t)
		gObjModel.erase(gObjModel.begin(),gObjModel.end());
	
	size_t = gObjWeapon.size();	
	if(size_t)
		gObjWeapon.erase(gObjWeapon.begin(),gObjWeapon.end());

} 



You'll notice that the vector is a member of a class...the class is static(one instance)...the program's "exit operation" destorys static classes last...

Any idea why this problem persists...I'm confident that the vector has been emptied before the error is thrown...also, if I do not fill these vectors, the error is not thrown upon exit...
You shouldn't need to erase like that. The vector has it's own destructor that is called, you don't need to do anything.

I tried to get around the problem by emptying the vector before its destruction...


That does nothing for the memory, and wouldn't fix your problem.

Where does the stack trace show the error is occurring?
Topic archived. No new replies allowed.