[C++-sig] Assert fail during deletion for an obj containing external object ptr

Gangadhar NPK npk.gangadhar at gmail.com
Mon Sep 18 16:20:21 CEST 2006


All,
I am a beginner for Boost.Python, so please be kind on the responses.
I am trying to create python wrappers for an existing library. I can't
change the source of the library ( I don't even have the src to
change). Also, since most of the classes are via factories, I can't
try to derive from the base classes, so am providing the wrappers by
containership. The python wrappers will contain the pointers to the
3rd party libs.

When I do this, I am able to get the interface to the third party
library's base interface, but during destruction (during the
interpretor exit), I get this assert failure:
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!
Program: c:\Python24\python.exe
File: dbgdel.cpp
Line: 51
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
---------------------------
Abort   Retry   Ignore
---------------------------
I understand that there might be a double delete or invalid deletion
that is happening, but I can't point exactly where. The code that
generates is attached as a CPP file.
This must be something basic that I am missing, but I can't figure out
from the boost.python documentation or the wiki. Can someone please
point at what I am doing wrong?

Thank You
Gangadhar
P.S:Also, ignore the fact that the 3rd party lib's static function is
not exported in the Wrapper class as a member function. That is
something that will be changed later.
-------------- next part --------------
#include <boost/python.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>

using namespace boost::python;
using namespace boost;

/**
* The code that is a 3rd party lib
*/
class ExternalLib
{
protected:
   ExternalLib(){};
public:
   static ExternalLib* Create() {return new ExternalLib();}
   std::string Print() {return "ExternalLib::Print";}
};

/**
* The wrapper to the above class
*/
class Wrapper
{
public:
   typedef Wrapper* Wrapper_ptr;
   typedef ExternalLib* ExternalLib_ptr;
   Wrapper_ptr GetWrapper()
   {
      ExternalLib_ptr m_external(ExternalLib::Create());
      return Wrapper_ptr(this);
   }
   
   std::string Print()
   {
      return m_external->Print();
   }
   ExternalLib_ptr m_external;
};

BOOST_PYTHON_MODULE(BoostTest)
{
   class_<Wrapper>("Wrapper")
      .def("GetWrapper",&Wrapper::GetWrapper,return_value_policy<manage_new_object>())
      .def("Print",&Wrapper::Print)
      ;
}


More information about the Cplusplus-sig mailing list