Deleting value stored inside a pointer

Started by
13 comments, last by DavidWolfire 11 years, 2 months ago

Ok I got it working thank you everyone for your advice, it really helped. Just one last question though: Even though the instance of the Monster disappears on screen (3d object is not rendered), is there a way to make sure the memory that was holding the instance is properly "cleaned"?

Aluthreney -- the King of sheep.

Advertisement
If you called delete on it, the memory of the object the pointer points to have been deleted. Furthermore, the destructor for the object will also be called before the actual memory is released so you can do additional cleanup there so no indirect resources are leaked.

Ok I got it working thank you everyone for your advice, it really helped. Just one last question though: Even though the instance of the Monster disappears on screen (3d object is not rendered), is there a way to make sure the memory that was holding the instance is properly "cleaned"?

No. It depends on the runtime and the operating system. For all you know it could be cached somewhere else in memory, or even in the swapfile on disk. But you should not have to even care what happened to the underlying memory, as long as you properly issued the delete statement indicating you no longer require use of this instance (unless you need it to be securely deleted, for security reasons or whatever).

In other words, if you've deleted the appropriate pointers, your worries end right there. From your perspective, everything happening at a lower level is assumed to work.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Depending on your platform and compiler you may be able to use a leak detector to determine if all your dynamically allocated memory has been properly freed. This thread contains some options. For non-memory resources there may be other tools available to check if they've been freed. (Ex: the DirectX debug runtimes will give you warnings if you haven't released everything and Windows Application Verifier can be used to detect leaked OS handles.)

If you call delete(ptr[5]), it is telling the OS that you no longer need the memory pointed to by ptr[5]. However! delete() will not re-initialize that memory for you. You can think of it like a sand castle -- when you call new(), you draw a square on the beach to mark off the area you will use for your castle. Inside the square, the sand starts out in some random state, all lumpy with footprints from everyone else walking all over the beach. Then you use a constructor or initializer to shape the memory, forming your sand castle. Since you have your lines drawn in the sand, everyone else avoids your sand castle, and only you are allowed to touch it.

When you call delete(), you erase the lines in the sand. Your sand castle is still there, and you can still point to it, but it is no longer marked as off limits for others. Eventually people will start walking through it and claiming that sand for themselves, and moving pieces of your castle around to make their own. You can still point to the spot your castle used to be at, but your castle may or may not still be there -- it could also contain bits and pieces of random castles created by other people.

In this metaphor, your question would be "How do I make my sand castle perfectly flat when I'm done with it?" You could do that using something like memset(ptr[5], 0xDD, sizeof(MyClass)), to overwrite all the memory used by MyClass to 0xDD, so that you can more easily detect if something is trying to access freed memory. This is a fairly advanced memory debugging technique though, which is only really useful if you are trying to track down a memory corruption error. Otherwise, re-initializing memory when deleting a pointer is a waste of developer and execution time.

You should also be aware that nobody else is re-initializing their memory when they free it, so whenever you block off some memory with malloc() or new(), it will start out in a random state -- it will not be nicely zeroed like you might expect. If you call int* test = new int, then *test will usually not equal zero. It will probably be some strange number, like -842150451, because that's what it was set to when some other function or program freed it.

This topic is closed to new replies.

Advertisement