** Changed in: python-apt (Ubuntu)
     Assignee: (unassigned) => Ubuntu Foundations Team (ubuntu-foundations)

-- 
Multiple memory leak in ALL versions of python-apt
https://bugs.launchpad.net/bugs/370149
You received this bug notification because you are a member of Ubuntu
Foundations Team, which is a bug assignee.

Status in “python-apt” source package in Ubuntu: New

Bug description:
Binary package hint: python-apt

This bug report references code from python/acquire.cc and python/generic.h.  I 
think it applies to *all* versions of python-apt.

As far as I can tell, every single use of the CppPyObject<T> template with a 
pointer type for the template parameter T results in a memory leak.  To give a 
concrete example, consider the use of the template in python/acquire.cc:

    pkgAcqFile *af = new pkgAcqFile(/* ... */);
    CppPyObject<pkgAcqFile*> *AcqFileObj = 
CppPyObject_NEW<pkgAcqFile*>(&PkgAcquireFileType);
    AcqFileObj->Object = af;

The function registered to deallocate the CppPyObject<pkgAcqFile*> is 
CppDealloc<>().  Here is its existing implementation:

    template <class T>
    void CppDealloc(PyObject *Obj)
    {
       GetCpp<T>(Obj).~T();
       PyMem_DEL(Obj);
    }

Notice that it calls the destructor for the contained object.  That works when 
T is a non-pointer type.  However, when T is a pointer type, the deconstructor 
being called is *that of the pointer, not what it points to*.  I.e. the 
destructor call resembles this:

    typedef char* T;
    T a = new char;
    a.~T();

Which of course doesn't really make sense.  Calling a.~T() doesn't free the 
memory pointed to by a.  So, when CppDealloc<>() is called with a pointer type, 
you probably want this:

    template <class T>
    void CppDealloc(PyObject *Obj)
    {
       delete GetCpp<T>(Obj)
       PyMem_DEL(Obj);
    }

I see several ways to fix this:
    1) don't use pointers with CppPyObject<>
    2) add a template specialization like CppPyObject<T*> that uses delete 
instead of calling the destructor directly
    3) use smart pointers (std::auto_ptr, or better, boost::shared_ptr) instead 
of naked pointers

The second option would be easy to implement, but what if you wanted a 
CppPyObject to contain a pointer without deleting it?  The first option is too 
restrictive.  Probably, the third option makes the most sense.

_______________________________________________
Mailing list: https://launchpad.net/~ubuntu-foundations
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~ubuntu-foundations
More help   : https://help.launchpad.net/ListHelp

Reply via email to