Leaky Destructors

Leaky Destructors

When dealing with inheritance, it is possible that the destructors for our objects may never be called if the class has virtual functions, but the base class has a non-virtual destructor. This leads to our derived classes being inadvertently leaky as we leak any memory allocated within the derived class.

A detailed description of this bug is available in a Microsoft DevBlog writeup available at:

Since this error is not always easy to detect when using code with higher levels of inheritance, or when deriving from third party libraries, it is a good thing that Visual Studio will generate warning about this potential memory leak.

Unfortunately, the warning which can catch this error condition is among those warning conditions which are disabled by default. Increasing your warning level will not find this condition as you have to turn it on manually using /we4265.

A full listing of warnings which are disabled by default is available at:

Leave a Reply

Your email address will not be published. Required fields are marked *