Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a heaped object in the Python wrapper generated with SWIG?

Tags:

c++

python

swig

I am developing a Python module along with a C++ library. In the C++ code, I have a function which returns a heaped object as follows.

MyClass* func()
{
  MyClass* myclass = new MyClass();
  return myclass;
}

But when I use this function on the Python side, I cannot delete the returned object.

myclass = func()
del myclass # still remains in memory

Could anyone tell me how to delete the returned object in a Python code, please?

I can change the type of the return value from MyClass* to MyClass so that I can avoid this memory leak. But I do not want to touch the C++ code, because the C++ library is already in use in another place.

like image 341
Akira Okumura Avatar asked Feb 17 '23 08:02

Akira Okumura


1 Answers

See the %newobject and %typemap(newfree) directives in section 11.2 of SWIG's documentation.

Quoted from docs:

A common problem in some applications is managing proper ownership of objects. For example, consider a function like this:

Foo *blah() {
   Foo *f = new Foo();
   return f;
}

If you wrap the function blah(), SWIG has no idea that the return value is a newly allocated object. As a result, the resulting extension module may produce a memory leak (SWIG is conservative and will never delete objects unless it knows for certain that the returned object was newly created).

To fix this, you can provide an extra hint to the code generator using the %newobject directive. For example:

%newobject blah;
Foo *blah();

%newobject works exactly like %rename and %exception. In other words, you can attach it to class members and parameterized declarations as before. For example:

%newobject ::blah();                   // Only applies to global blah
%newobject Object::blah(int,double);   // Only blah(int,double) in Object
%newobject *::copy;                    // Copy method in all classes
...

When %newobject is supplied, many language modules will arrange to take ownership of the return value. This allows the value to be automatically garbage-collected when it is no longer in use. However, this depends entirely on the target language (a language module may also choose to ignore the %newobject directive).

Closely related to %newobject is a special typemap. The "newfree" typemap can be used to deallocate a newly allocated return value. It is only available on methods for which %newobject has been applied and is commonly used to clean-up string results. For example:

%typemap(newfree) char * "free($1);";
...
%newobject strdup;
...
char *strdup(const char *s);

In this case, the result of the function is a string in the target language. Since this string is a copy of the original result, the data returned by strdup() is no longer needed. The "newfree" typemap in the example simply releases this memory.

like image 134
Mark Tolonen Avatar answered Apr 09 '23 03:04

Mark Tolonen