Search code examples
c++pythonboostvirtual-destructor

Destructor and python


I have some Base class in c++. I export it into python using boost::python. But what about virtual destructor? Any base class should have virtual destructor to avoid wrong memory freeing, right? I forget about that and wrote Base without destructor. Everything works, but with a lot of memory leaks.

Now I've added:

class Base
{
   public:
      virtual ~Base();
      // Other members...
};

And after importing of my module in python I get an error:

ImportError: Base.so: undefined symbol: _ZTI6Base

What I'm doing wrong? And, as I understand, the error causes due to missing destructor exporter for py-module.


Solution

  • The missing symbol error is probably caused by you failing to define the destructor (you're declaring the destructor, but it's unclear from your question whether you're defining it):

    class Base
    {
       public:
          virtual ~Base() {}
          // Other members...
    };
    

    (note the added curly braces)

    As to your question about whether every "base class should have virtual destructor to avoid wrong memory freeing", please take a look at the FAQ: http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7