简体   繁体   English

C ++ Python包装器中的内存泄漏

[英]Memory leak in c++ python wrapper

I'm develloping the backend of an app and I try to wrap my c++ code in python. 我正在开发应用程序的后端,然后尝试将c ++代码包装在python中。 I've used Boost Python3 to link c++ to python. 我已经使用Boost Python3将c ++链接到python。 I'm able to get a shared library and call it from python. 我能够获得一个共享库并从python调用它。 For the moment, everything is working. 目前,一切正常。

The problem arises when I'm trying to export this library. 当我尝试导出该库时出现问题。 I would like to be able to use it from another location or computer without recompiling the c++ code. 我希望能够在其他位置或计算机上使用它,而无需重新编译c ++代码。

To try this library, I'm just moving the library in another folder with its depedencies and check with ldd if all the depedencies are resolved (no problem for that). 要尝试该库,我只是将库移动到另一个具有其依赖关系的文件夹中,并使用ldd检查所有依赖关系是否都已解决(这没有问题)。

Then, I'm trying to call some object from python3. 然后,我试图从python3调用一些对象。 At the beginning, I'm able to run many functions, but if I quit and relaunch python3, I start to have some segmentation fault, memory corruption, ... As an example: * Error in `python3': free(): invalid next size (normal): 0x0000000001ebeb50 * 一开始,我能够运行许多功能,但是如果我退出并重新启动python3,我就会遇到一些分段错误,内存损坏,...例如: * python3中的错误:free():下一个无效尺寸(正常):0x0000000001ebeb50 *

I've tried to use valgrind to find any memory leaks. 我尝试使用valgrind查找任何内存泄漏。 My program in c++ doesn't have any memory leak. 我的C ++程序没有任何内存泄漏。 When I try valgrind with my python code, I don't have any leaks for the library located in its original folder. 当我用python代码尝试valgrind时,位于其原始文件夹中的库没有任何泄漏。 However, after having moved the library, I start to have some leaks as: 但是,在移动了库之后,我开始出现一些泄漏:

Invalid write of size 4 ==22695== at 0x6DCA0F9: Test::Test(std::string, std::string, std::string, int) (maintests.cpp:71) ==22695== by 0x6933E5B: boost::python::objects::value_holder<Test>::value_holder(_object*) (value_holder.hpp:137) ==22695== by 0x6934D8D: boost::python::objects::make_holder<0>::apply<boost::python::objects::value_holder<ritmo::Test>, boost::mpl::joint_view<boost::python::detail::drop1<boost::python::detail::type_list<boost::python::optional<std::string, std::string, std::string, int, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_>, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_> >, boost::python::optional<std::string, std::string, std::string, int, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_> > >::execute(_object*) (make_holder.hpp:94) ==22695== by 0x693924E: _object* boost::python::detail::invoke<int, void (*)(_object*), boost::python::arg_from_python<_object*> >(boost::python::detail::invoke_tag_<true, false>, int const&, void (*&)(_object*), boost::python::arg_from_python<_object*>&) (invoke.hpp:81) ==22695== by 0x6936942: boost::python::detail::caller_arity<1u>::impl<void (*)(_object*), boost::python::default_call_policies, boost::mpl::vector2<void, _object*> >::operator()(_object*, _object*) (caller.hpp:223) ==22695== by 0x6935D88: boost::python::objects::caller_py_function_impl<boost::python::detail::caller<void (*)(_object*), boost::python::default_call_policies, boost::mpl::vector2<void, _object*> > >::operator()(_object*, _object*) (py_function.hpp:38) ==22695== by 0x71CE139: boost::python::objects::function::call(_object*, _object*) const (in /usr/lib/x86_64-linux-gnu/libboost_python-py34.so.1.54.0) ==22695== by 0x71CE4A7: ??? (in /usr/lib/x86_64-linux-gnu/libboost_python-py34.so.1.54.0) ==22695== by 0x71D8742: boost::python::handle_exception_impl(boost::function0<void>) (in /usr/lib/x86_64-linux-gnu/libboost_python-py34.so.1.54.0) ==22695== by 0x71CCDB2: ??? (in /usr/lib/x86_64-linux-gnu/libboost_python-py34.so.1.54.0) ==22695== by 0x53493C: ??? (in /usr/bin/python3.4) ==22695== by 0x4F14F9: PyObject_Call (in /usr/bin/python3.4) ==22695== Address 0x6333fe0 is 16 bytes after a block of size 32 in arena "client"

I'm struggling with this issue. 我正在努力解决这个问题。 Any idea or tips will be more than welcome. 任何想法或技巧都将受到欢迎。

Thank you 谢谢

I have finally found the segfault. 我终于找到了segfault。 The header I was using for compiling the program was different from the one used by the library. 我用于编译程序的标头与库使用的标头不同。 One class member was not declared and so no memory was allocated to this member. 没有声明一个类成员,因此没有内存分配给该成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM