yuvi
yuvi

Reputation: 1092

missing destructor declaration

Can anyone tell me what happens with object's memory if I forget to declare a destrucor in a C++ class? I mean, whether it is freed or causes memory leak? An example or demo will be appreciated.

Thanks in advance.

Upvotes: 2

Views: 2770

Answers (3)

Peter Bloomfield
Peter Bloomfield

Reputation: 5766

It's often considered good practice to define a destructor for any non-trivial class (see the Rule of Three). However, in modern C++ (i.e. C++11 onwards), it's not as necessary as it used to be.

If your class is not inherited from anything else, any direct members (such as variables and objects) will be properly destroyed by the default destructor provided by the compiler. Similarly, if your object owns any heap-allocated objects which are wrapped in smart pointers, they will be destroyed safely too.

A problem arises if your object owns any heap-allocated data via raw pointers. The implicit destructor has no way of knowing what to do with them, so you will need a custom destructor to clear them up. For example:

class MyClass
{
    int m_data1;
    std::string m_data2;
    std::shared_ptr<Widget> m_data3;
    Widget *m_data4;
};

In the above example, members m_data1, m_data2, and m_data3 will all be cleared-up correctly without a custom destructor. However, the object pointed to by m_data4 will not be cleared up automatically. If it was allocated by MyClass, then it will usually result in a memory leak (unless it's getting freed by something else).

With all of that said, inheritance changes things in an important way. If your class is inherited by anything else then you should probably always give it a virtual destructor. If your object is deleted via a pointer to an inherited class, and that class does not have a virtual destructor, then the sub-class' destructor will never be called, potentially resulting in memory leaks.

For example:

class Parent
{
public:
    Widget m_data1;
}

class Child : public Parent
{
public:
    Widget m_data2;
}

int main()
{
    Parent *ptr = new Child;
    delete ptr; // <-- clears-up Parent but not Child
}

In the above example, ptr is of Parent type, so the delete only knows about the Parent part of the object. That means only m_data1 will be cleared-up correctly. Without a virtual destructor, it doesn't know about the Child part of the object, so m_data2 will not be cleared-up correctly (its destructor will never be called).

Upvotes: 7

juanchopanza
juanchopanza

Reputation: 227558

It depends on the class' data members. If the class manages resources, then it needs a destructor to release them (you should also provide a copy constructor and assignment operator, or make the class non-copyable and non-assignable).

If the class has built-in data members, or data members that manage their own resources, then the implicitly generated destructor is enough. It will call the destructor of all data members.

Upvotes: 2

ForEveR
ForEveR

Reputation: 55897

If destructor is not declared, compiler will generate destructor, that will call destructors of all members of object. Leak can be only if you are working with raw-memory (C files, memory allocation etc).

Upvotes: 2

Related Questions