As I was worried about if the class goes out of scope and I haven't deleted map. Then, wouldn't that cause a memory leak.
You're right - you should delete map
exactly as your code does. But, you should also make your object non-copyable (derive from boost or C++11 noncopyable base classes, or add a private declaration (with no definition/implementation) of the operator=
copy assignment and copy constructor. Otherwise, if you (deliberately or accidentally or incidentally - e.g. when storing your object in a container that sometimes copies it around, such as a std::vector
) copy your object, then the first copy destructored will delete
the map
, any other copy that tries to use it will likely crash, and any other copy's destructor that also tries to delete
it will also have undefined behaviour.
Do I have to manually call to delete map.
Yes, in your code you do.
A better alternative is to use a smart pointer whose own destructor will delete the pointed-to object.
As I get crash if I call delete in the constructor in my program.
Well, if you call delete
after the new
, then the constructor won't crash, but you wouldn't have a map
object to use after the constructor returns, and if you then try to delete
it again in the destructor the you get undefined behaviour which may well manifest as a crash.
If you want to delete
the map earlier than the destructor sometimes, you can set the pointer to NULL
so that a future delete
will do nothing safely. You should then check for NULL before trying to use the map
.
Like I could add a method to Level called say destroy map where I delete map. But, was wondering why I can't add delete into the destructor.
As above, you can have destroy_map
, but it must coordinate with the destructor.
When there's no compelling reason to do otherwise, it's better to make the map
member data, not storing it by reference. When a pointer is useful, use a smart pointer if at all possible. If you want to implement explicit manual memory management, be wary of the issues above.