简体   繁体   中英

Memory leak in c++ python wrapper

I'm develloping the backend of an app and I try to wrap my c++ code in python. I've used Boost Python3 to link c++ to python. I'm able to get a shared library and call it from 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.

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).

Then, I'm trying to call some object from 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 *

I've tried to use valgrind to find any memory leaks. My program in c++ doesn't have any memory leak. When I try valgrind with my python code, I don't have any leaks for the library located in its original folder. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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