Memory management is almost automatic in C ++ (with a few caveats).
In most cases, do not dynamically allocate memory.
Use local variables (and regular member variables), and they will automatically create and destroy.
When you need pointers, use a smart pointer.
Start by using boost :: shared_pointer <T> instead of pointers.
This will lead you to the right path and stop mistakenly deleting memory at the wrong time, and 90% of the code will be correctly issued (unfortunately, loops can cause a problem (only from the point of view of leaks), and you will need to develop accordingly (but we have other smart pointers for handling loops weak_ptr))
My fundamental rule is that a class never contains a RAW pointer. Always use some standard container or smart pointer. Using them; destructor calls become automatic.
As soon as you start reading other smart pointers and when to use them:
Smart pointers: or to whom does the child belong?
Martin york
source share