According to: http://www.cplusplus.com/doc/tutorial/classes/
The destructor fulfills the opposite functionality of the constructor. It is automatically called when an object is destroyed, either because its scope of existence has finished (for example, if it was defined as a local object within a function and the function ends) or because it is an object dynamically assigned and it is released using the operator delete.
Example code:
class Something
{
public:
Something() {cout << "construction called" << endl; }
~Something() { cout << "destruction called" << endl; }
};
void foo(){
Something *ob = new Something();
}
int _tmain(int argc, _TCHAR* argv[])
{
foo();
}
That’s true, but you allocate the object in dynamic memory, which means it won’t be destructed until you call
delete.You never call delete. Ergo memory leak and uncalled destructor:
Either this, or simply allocate the object in automatic memory:
or delegate the memory management to a smart pointer.