c – 如何删除使用SWIG生成的Python包装器中的堆积对象?

我正在开发一个
Python模块和一个C库.在C代码中,我有一个函数,它返回一个堆积的对象,如下所示.

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

但是当我在Python端使用这个函数时,我无法删除返回的对象.

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

有谁能告诉我如何删除Python代码中返回的对象,好吗?

我可以将返回值的类型从MyClass *更改为MyClass,以便我可以避免此内存泄漏.但是我不想触及C代码,因为C库已经在另一个地方使用了.

最佳答案 请参阅SWIG文档的第
11.2节中的%newobject和%typemap(newfree)指令.

引自文档:

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.

点赞