trouble using "delete"

Started by
7 comments, last by thuned 22 years, 9 months ago
here's a piece of my code. uncommenting the delete would lead to crashing but i have no idea y
  
CTextureManager::CTextureManager()
{
	TextureList = new CBinaryArray<CTexture>(Compare);
}

CTextureManager::CTextureManager(GLuint size)
{
	TextureList = new CBinaryArray<CTexture>(Compare,size);
}

CTextureManager::~CTextureManager()
{
	/*
	if (TextureList)
		delete TextureList;
	/**/
}
   
i don't know if this matters but i declared by using this
  
CBinaryArray<CTexture>* TextureList;
   
life is unfair, take advantage of it. UNMB2 - if the link doesn't work, try clicking it Edited by - thuned on July 21, 2001 10:41:11 PM
life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)
Advertisement
First thing, something simple to help keep your code clean. Rather than this:
  if (TextureList)   delete TextureList;  

you can safely do this:
  delete TextureList;  

since the language states that delete NULL is okay to do.

Now, as to your problem, I don't see anything wrong offhand. Perhaps something in the CBinaryArray class is causing a problem? Is it your class or a library? If yours or if you have the library source, I suggest using the debugger to step into the delete function and find out what specifically is causing the problem. Debuggers also usually have the ability to break on exception, to help find problems such as this.


---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!


Edited by - mossmoss on July 21, 2001 12:36:04 AM
If it crashes when you call delete, then it''s usually a sign that you''ve written past the end of the allocated memory. The problem with memory accesses is that in most cases, it difficult to figure out if you''ve written one or two bytes past the end of the memory (unless they go over a page boundary) so the debug new will add an extra few bytes to the start and end of the memory block, and delete will check if you''ve changed any of those values. If you have, you''ll get a crash.

Debugging these problems is extremely difficult (unless you''re lucky enough to own BoundsChecker or some similar software).

I can''t really give you any solution, except check (throroughly!) for memory over/under runs.

War Worlds - A 3D Real-Time Strategy game in development.
Make sure you are not changing the address of TextureList anywhere. An easy way to check this is to create another pointer and make newpointer = TextureList in your constructor after you call new TextureList. Then in your destructor check to see that newpointer still == TextureList before deleting. If they are not equivalent then either you are changing (possibly incrementing?) TextureList, or something is overwriting the memory that holds your TextureList pointer.

Seeya
Krippy
quote:Original post by Krippy2k
Make sure you are not changing the address of TextureList anywhere. An easy way to check this is to create another pointer and make newpointer = TextureList in your constructor after you call new TextureList. Then in your destructor check to see that newpointer still == TextureList before deleting. If they are not equivalent then either you are changing (possibly incrementing?) TextureList, or something is overwriting the memory that holds your TextureList pointer.

Seeya
Krippy


what happens if i do change it?
  BOOL CTextureManager::SetAmount(GLuint amount){	return pTextureList->Resize(amount);}  

that creates a new vector, copies the old one over, and then deletes the old one, then set the pointer to the new one. could that be the problem?

life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)
and how do i check for memory over/under runs?
and i don''t really know how to debug. well, for console based i have no problems, but win32 debug doesn''t seem to work for me
life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)
The best way to test for memory over and under runs is to overload new and delete. I''m sure there''s other ways, but this is probably the easiest (and most flexible). In your new version of new, just allocate, say, 32 extra bytes and fill them with a certain character (not 0, but anything else should be fine) then return a pointer that''s 16 bytes into your allocated block. In the delete operator, check the 16 bytes before and 16 bytes after (you''ll have to store the length of the memory as well) and make sure they''re the same. If they''re not the original character you put there, then there''s been an overrun. There''s no way to (easily) test what caused the over run, unless you''ve got something like BoundsChecker, so to figure out what''s actually causing it will take several hours of just staring at the code

It''s probably tempting at this point to leave the delete commented out and maybe come back to the problem later, but I strongly suggest you figure out the bug now because it''s probably a symptom of a much bigger problem...

War Worlds - A 3D Real-Time Strategy game in development.
What is the relationship of pTextureList to TextureList? Not sure exactly what''s going on there. lol

If you delete something, reallocate it and set the pointer to the newly allocated object, then there should not be a problem.

If your pointer is pointing to memory that was already deleted, it will crash. If your pointer is pointing to somewhere within an allocated block of memory other than the beginning of that allocated block it will crash. If it is pointing to the beginning of a previously allocated chunk of memory you should be ok.

Seeya
Krippy
thanks all!
i fixed it.
somewhere else in my code i called delete but forgot to set it pointing to null
life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)

This topic is closed to new replies.

Advertisement