[C++-sig] Memory Leaks In VS.NET 2003 With BOOST_PYTHON_MODULE

Matt mjkeyes at sbcglobal.net
Fri Oct 7 20:30:17 CEST 2005


One more quick note - in doing some google searches on this subject,
I found a couple of things:

1.  People were told to ignore the boost memory leaks because modern
computers can handle it (which sounds like a BAD BAD BAD approach
to me)
2.  People were told not to call Py_Finalize()

Are either of these correct, and can I ignore my Py_Finalize call?

"Ralf W. Grosse-Kunstleve" <rwgk at yahoo.com> wrote in message 
news:20051007163924.10208.qmail at web31514.mail.mud.yahoo.com...
> Your posting is missing new-lines and therefore very difficult to read.
>
> Over the last couple of year I ran our 50+ Boost.Python extension 
> repeatedly
> with valgrind, incl leak check. It is a very long time ago (3+ years) that 
> I
> found the last memory leak in Boost.Python. If there are still memory 
> leaks in
> Boost.Python they must be in very obscure places. I'd bet that any leaks 
> you
> are seeing are in your code. Seeing manual INCREF and DECREF in your code 
> makes
> this seem all the more likely to me. I'd consider using 
> boost::python::handle<>
> and boost::python::object to automate the reference counting.
>
> The key to resolving issues like your is always the same: carefully reduce 
> your
> code just until the error goes away. Then put back the last code removed 
> so
> that the error appears again. Keep reducing the code until you have a 
> minimal
> example. If the problem  is not in your code, post the minimal example to 
> this
> list, complete with information about your platform and instructions how 
> to
> compile.
>
> Cheers,
>        Ralf
>
> --- Matt <mjkeyes at sbcglobal.net> wrote:
>
>> Hey all,
>>
>> i know i just posted about something else yesterday, but i have found a
>> number of memory leaks that crop up when i use BOOST_PYTHON_MODULE. 
>> Here's
>> what it looks like:
>>
>> //NOTE SocketLib and PythonLib are two of my namespaces
>>
>> BOOST_PYTHON_MODULE(PyInputHandler)
>> {
>>
>>
> class_<PythonLib::PythonInputHandler,boost::noncopyable>("PythonInputHandler")
>>
>>
> .def("HandleInput",pure_virtual(&SocketLib::InputHandler<std::string>::HandleInput))
>>  .def("Enter", 
>> pure_virtual(&SocketLib::InputHandler<std::string>::Enter))
>> .def("Leave", pure_virtual(&SocketLib::InputHandler<std::string>::Leave))
>> .def("Hungup",pure_virtual(&SocketLib::InputHandler<std::string>::Hungup))
>> .def("Flooded",pure_virtual(&SocketLib::InputHandler<std::string>::Flooded))
>> .def("NoRoom",pure_virtual(&SocketLib::InputHandler<std::string>::NoRoom))
>> .def("Quit", &PythonLib::PythonInputHandler::Quit)  .def("ShutDown",
>> &PythonLib::PythonInputHandler::ShutDown)  .def("SendGlobal",
>> &PythonLib::PythonInputHandler::SendGlobal)  .def("AddInputHandler",
>> &PythonLib::PythonInputHandler::AddInputHandler)  ;}The leaks (17 of 
>> them)
>> come from the init_module_PyInputHandler call that isgenerated from this
>> macro.Here is what I call to instantiate my Python class in C++: 
>> PyObject
>> *pInit = PyImport_I!
>>  mportModule( "Python.SnakeMUD" );  if( pInit == NULL )  { 
>> PyErr_Print();
>>     //throw custom exception handling  }  //call the python method to 
>> create
>> our initial input handler  handle<> hClassPtr(PyObject_CallMethod(pInit,
>> "CreateInitialHandler",NULL));  //wrap the handle in a boost class 
>> object
>> Handler(hClassPtr);  //Get out class out of the boost class
>> PythonLib::PythonInputHandler *pHandler
>> =extract<PythonLib::PythonInputHandler*>(Handler);  //this will call
>> Py_XINCREF on the PyObject *, which will allow ourhandler to continue to
>> exist after this function exits  pHandler->SetPyObject(Handler.ptr());
>> //cleanup the module call  Py_XDECREF(pInit);  //add our new handler
>> AddInputHandler(pHandler);Nothing really fancy unless i'm doing something
>> completely stupid.  Anyadvice?Thanks in advance!Matt
>
>
>
>
> __________________________________
> Yahoo! Mail - PC Magazine Editors' Choice 2005
> http://mail.yahoo.com 






More information about the Cplusplus-sig mailing list