检测到 Swig/Python 内存泄漏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/918180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:07:11  来源:igfitidea点击:

Swig / Python memory leak detected

pythonmemory-leaksswig

提问by

I have a very complicated class for which I'm attempting to make Python wrappers in SWIG. When I create an instance of the item in Python, however, I'm unable to initialize certain data members without receiving the message:

我有一个非常复杂的类,我试图在 SWIG 中为它制作 Python 包装器。但是,当我在 Python 中创建项目的实例时,我无法在未收到消息的情况下初始化某些数据成员:

>>> myVar = myModule.myDataType()
swig/python detected a memory leak of type 'MyDataType *', no destructor found.

Does anyone know what I need to do to address this? Is there a flag I could be using to generate destructors?

有谁知道我需要做什么来解决这个问题?有没有我可以用来生成析构函数的标志?

回答by ASk

SWIG always generates destructor wrappers (unless %nodefaultdtordirective is used). However, in case where it doesn't know anything about a type, it will generate an opaque pointer wrapper, which will cause leaks (and the above message).

SWIG 始终生成析构函数包装器(除非使用%nodefaultdtor指令)。但是,如果它对某个类型一无所知,它将生成一个不透明的指针包装器,这将导致泄漏(以及上述消息)。

Please check that myDataTypeis a type that is known by SWIG. Re-run SWIG with debug messages turned on and check for any messages similar to

请检查这myDataType是 SWIG 已知的类型。在打开调试消息的情况下重新运行 SWIG 并检查是否有任何类似于以下内容的消息

Nothing is known about Foo base type - Bar. Ignored

Receiving a message as above means that SWIG doesn't know your type hierarchy to the full extent and thus operates on limited information - which could cause it to not generate a dtor.

收到上述消息意味着 SWIG 不完全了解您的类型层次结构,因此对有限的信息进行操作 - 这可能导致它不生成 dtor。

回答by hhafez

The error message is pretty clear to me, you need to define a destructor for this type.

错误信息对我来说很清楚,你需要为这种类型定义一个析构函数。