C, C++, C++/CLI

C++ Tutorial – Almost error cases that you got when try to delete a pointer

Without some code of where things are crashing, we can only speculate.  There are a million things that could be going wrong, such as

  • Deleting memory twice (especially likely if you copy pointers around)
  • Deleting a pointer that was never initialized
  • Deleting a pointer that was assigned to garbage
  • Deleting a pointer to memory that was allocated with something other than new
  • Deleting a pointer that points to an item not on the heap (i.e. an item on the stack or a global variable)
  • If you have an array of pointers, deleting a pointer off the end of the array
  • Deleting a pointer that was allocated from another instance of the C++ runtime (e.g. calling new from a DLL and delete from the main exe or visa versa)
  • Calling delete[] on memory allocated with new.  Calling delete on memory allocated with new[] (These may not always crash, but it is still wrong and undefined behavior).

I am sure there are more that I didn’t think of off the top of my head.

 

From http://social.msdn.microsoft.com

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s