Skip to content

Deep understanding and real time examples of memory leak with c++

Notifications You must be signed in to change notification settings

ramkumarrammohan/memory_leak_concepts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

memory_leak_concepts

Deep understanding and real time examples of memory leak with c++

Here are the list of some memory leaks that everyone needs to take care while developing applications using cpp

Memory leak with inheritance

Everyone needs to cross inheritance anywhere in their projects. While dealing with inheritance we need to make sure both base and derived objects should not create any memory leaks. The main problem starts with base class destructor part. Whenever we are assigning some derived instance to base pointers we should take care to destruct both base and derived instances fully.

class BaseClass
{
        BaseClass();
    BaseClass(); // Destructor with mistake
    // virtual BaseClass(); // Correct way
}

class DerivedClass : public BaseClass
{
        DerivedClass();
    ~DerivedClass(); // Destructor
}

void main()
{
        BaseClass *ptr = new DerivedClass();
        // some operations with ptr
        delete ptr; // this will call the destructor ~Base() and NOT ~Derived()
}

In above case the deletion of ptr only deletes base instance alone. It wont take care of its own to delete derived instance. So your DerivedClass instance still occupy the heap.

To avoid this problem make your base destructor to be a virtual(virtual ~BaseClass();) one so that at runtime while deleting the base instancedelete ptr automatically deletes the derived instances.

Full Example Here goes here: Memory leak with Inheritance

To play with example follow steps mentioned here Tips to check behaviour (README)

About

Deep understanding and real time examples of memory leak with c++

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published