Boost C++ Libraries: Ticket #3716: Application crash when using Boost.Python in a plug-in DLL for that application https://svn.boost.org/trac10/ticket/3716 <p> I am developping a DLL for a Win32 application that already host a Python interpreter (Python 2.5.x) which I extend using Boost.Python. </p> <p> As far as Boost.Python is concerned, the sequence of operations (which I can't change) is : </p> <ol><li>App calls Py_Initialize </li></ol><ol start="2"><li>App loads my DLL </li></ol><ol start="3"><li>my DLL extend Python with a "boost module" (such as the "Hello World" example in the documentation </li></ol><ol start="4"><li>App notifies my DLL that it is about to be unloaded </li></ol><ol start="5"><li>App unloads my DLL </li></ol><ol start="6"><li>App calls Py_Finalize... and crash! </li></ol><p> The application doesn't use Boost.Python, and doesn't even share the C run-time (i.e. different heaps). Whether I use static or dynamic linking of Boost.Python doesn't seem relevant in that case. Yes, I know, Boost.Python doc states that Py_Finalize should not be called. But in that case, it IS called after my DLL is unloaded and I can't do anything about it (and I'd like to use Boost.Python in such a scenario). </p> <p> Cause : </p> <p> I manage to pinpoint where it fails from Py_Finalize. At that point, the 'Boost.Python.function' python object (defined in the function_type global variable in libs/python/src/object/function.cpp) is internally referenced by Python (by base class type among other things) and Python eventually tries to access it (at least change its ref count I guess). However, since the DLL is already unloaded, the memory that held the global variable is not mapped anymore. </p> <p> Resolution : </p> <p> Not being a Python expert, I haven't found a way to remove references to that 'Boost.Python.function' when my DLL is notified about being unloaded. I'd be glad to know if there is. However, it was fairly simple to get Python to allocate this object in its own heap, so that it lives as long as the interpreter. Attached is the patch file for that fix. I can't tell though how "clean" or not is copying a complete '<a class="missing wiki">PyTypeObject</a>' structure instance over a somewhat-already-initialized instance from PyObject_New()... but that seems to work. </p> <p> Note that to avoid the crash, I also need to "undef" all function definitions from the boost module with PyObject_DelAttr() (else Py_Finalize accesses them but they're from a heap that was deallocated from memory when the DLL was unloaded). That does not however require any modification to Boost.Python. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3716 Trac 1.4.3 Emmanuel Giasson <zirconia_@…> Fri, 04 Dec 2009 17:43:36 GMT attachment set https://svn.boost.org/trac10/ticket/3716 https://svn.boost.org/trac10/ticket/3716 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost_python_function.patch</span> </li> </ul> <p> Potential way to fix that issue </p> Ticket Emmanuel Giasson <zirconia_@…> Fri, 04 Dec 2009 18:01:00 GMT <link>https://svn.boost.org/trac10/ticket/3716#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3716#comment:1</guid> <description> <p> I just realized that my fix may cause another bug should the application "reset" the Python interpreter (such as calling Py_Finalize then Py_Initialize) while the DLL with Boost.Python is still loaded. The pointer in function.cpp on the Boost.Python.function object then becomes a dangling reference. Perhaps a cleanup function registered with Py_AtExit could fix that. </p> </description> <category>Ticket</category> </item> <item> <author>erkki.vahala@…</author> <pubDate>Tue, 23 Feb 2010 06:15:56 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3716#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3716#comment:2</guid> <description> <p> Boost python has also other problems related to initialization via static variables: on Windows, loading a DLL, which defines boost python modules with BOOST_PYTHON_MODULE macros, causes RTTI data to be stored in a static registry (converter/registry.cpp). If the DLL is then unloaded, registry will contain danging pointers - subsequent attempts to load DLLs with modules/exercise the registry by other means will cause a memory access violation as the registry tree comparison operations try to access the stale RTTI data. </p> </description> <category>Ticket</category> </item> <item> <author>Amaury Forgeot d'Arc <amauryfa@…></author> <pubDate>Tue, 20 Apr 2010 17:57:02 GMT</pubDate> <title>cc set https://svn.boost.org/trac10/ticket/3716#comment:3 https://svn.boost.org/trac10/ticket/3716#comment:3 <ul> <li><strong>cc</strong> <span class="trac-author">amauryfa@…</span> added </li> </ul> <p> Unloading a Python extension module is not supported. Even if you manage to copy the type, all its members (even tp_name!) are invalid, and will lead to a crash. </p> <p> Static types are static; this is a limitation of CPython, not a defect of Boost.Python. </p> Ticket