Incomplete Type memory leaks?

c++, memory-leaks, visual-c++

Solution

It often happen when using Pimpl, so I'll focus on the solution there:

class FooImpl;

class Foo
{
public:
  // stuff
private:
  Pimpl<FooImpl> m_impl;
};

The problem here is that unless you declare a destructor, it will be automatically generated, inline, by the compiler. But of course, the compiler will have no idea of the complete type of `FooImpl` there.

You thus have to explicitly define the destructor, even if empty, and put the definition somewhere where the complete type of `FooImpl` is visible.

// cpp file
class FooImpl
{
};

Foo::~Foo() {} // Empty, but now correctly generated
               // because FooImpl complete at this point.

Also, if like me you defined your `Pimpl` class to be pretty smart (regarding construction, copy and assignment), then those will also need to be defined in the .cpp file.

It's really a hassle, but then you have nicely encapsulated your implementation details, so I suppose it's worth it.

Problem

Microsoft Visual Studio 2008 is giving me the following warning: warning C4150: deletion of pointer to incomplete type 'GLCM::Component'; no destructor called This is probably because I have defined Handles to forward declared types in several places, so now the Handle class is claiming it won't call the destructor on the given object. I have VLD running and I'm not seeing any leaks. Is this literally not calling the destructor for this object or is this a "may not call destructor for object" warning? Yet another memory leak question from me, haha.

Original source