[C++-sig] Nasty Heap Error in MSVC 7.1 With Boost and Python Using Derived Classes

Matt mjkeyes at sbcglobal.net
Thu Oct 6 15:33:00 CEST 2005

Hey all,

i'm trying to create an application that both embeds and extends python
through boost.  i've got a rough framework up and running, but now that
i'm finally to the python part i'm having troubles.  Please know that i'm a
noob to boost and to boost.python, so if you see any obvious noob
idiocies in my code please point them out (we've all got to start learning
somewhere :)).

Here's a rough run-down of my code:

Base class (pure):

template<class commandtype>
 virtual void Enter(commandtype *pOutput) = 0;

Derived class:
class PythonInputHandler : public InputHandler<std::string>
 PythonInputHandler(PyObject *pSelf) : m_pSelf(pSelf) {}
 PythonInputHandler(const PythonInputHandler &rhs)
 : InputHandler<std::string>(rhs) //doesn't really do anything
 , m_pSelf(rhs.m_pSelf)
 virtual ~PythonInputHandler() {Py_XDECREF(m_pSelf);}

 //overriden pure function:
 virtual void Enter(std::string *pOutput)
   //this is where the nasty heap crash comes in, more below
   *pOutput = call_method<std::string>(m_pSelf, "Enter");

 class_<InputHandler<std::string>, PythonInputHandler,

Now, when the executable runs, this happens:
 if( PyImport_AppendInittab( "PyInputHandler", initPyInputHandler ) ==
-1 )
 throw runtime_error("blah");


 PyObject *pInit = PyImport_ImportModule("Python.MyTest");
 if(pInit == NULL)
   //...error handling

 PythonInputHandler *pHandler = new

 std::string sTest;
 pHandler->Enter(&sTest); //big heap crash


Here is the Python code:
from PyInputHandler import *

class PyLogonHandler(PythonInputHandler):
       def Enter(Output):
               return "Welcome from Python!"

def CreateInitialHandler():
       return PyLogonHandler()

Now then, if i change "Welcome from Python!" to a shorter string, like
"Hiya!", then it works, no error.  As it stands, i get a
_BLOCK_TYPE_IS_VALID error in what looks like the destructor of the
std::string during the *pOutput = call_method... portion of the code
(i'm assuming the destructor before the assignment to pOutput occurs).
However, in the destructor, i do see the data consisting of "Welcome
from Python!"

Now i'm also a complete noob to boost and python.  i've only been
working on this for about 5 hours or so (the python side of things at
any rate), so any tips to improve what i'm doing in addition to helping
with this error is greatly appreciated.  i'm finding it difficult to
find some boost.python tutorials that are comprehensive enough.  If you
can't tell, i'm trying to expose a version of my InputHandler class to
python so that it can be overriden and created from scripts instead of
being hard-coded into the C++.

Thanks for the help!!!!

More information about the Cplusplus-sig mailing list