All good C ++ programmers know how to avoid memory leaks (or resources such as sockets):
std::auto_ptr
boost::shared_ptr
But memory leaks are still occurring. Indicate the most common problems when you find a memory leak in a program, even if you used the above methods.
I start:
Sometimes you forget to define the base class destructor as virtual. Thus, all derived classes refer to a pointer to a base class that was not correctly destroyed and therefore leaked.
There are many other types of errors than just leaks. In order from worst to best:
Data is stored in an area where it should not be. This leads to both the majority of security problems and the best, most difficult to track.
X
new
free
malloc
delete
, , .
new[]
delete[]
smart_ptr
weak_ptr
, std::auto_ptr boost::shared_ptr (2) , .
, , ( smartpointers):
class A { public: int avery_useful_calculation()const { return 4; } private: // class shouldn't be instantiated as an automatic variable (on stack) virtual ~A(){} }; // client code: needs a very useful calculation int i = (new A)->avery_useful_calculation();
, , , "" .
"" , try {} __finally {}, ( -, , -)
try {} __finally {}