|
Boost : |
From: Kim Chang Han (chkim_at_[hidden])
Date: 2003-02-26 21:55:50
Here is my simple test code modified from embedding.cpp test code(by Dirk Gerrits).
I successfully compiled, but my excutable raise heap error assertion when the time freeing the string result of the function py.hello().
Can I prevent this error?
My sampel code and result is below.
// main.cpp
// Copyright David Abrahams 2002. Permission to copy, use,
// modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
// embedded_hello -- A simple Boost.Python embedding example -- by
// Dirk Gerrits
#include <iostream>
#include <stdexcept>
#include <boost/python.hpp>
#include <boost/scoped_ptr.hpp>
namespace python = boost::python;
// An abstract base class
class Base //: public boost::noncopyable
{
public:
virtual ~Base() {};
virtual std::string hello() { return "base"; }
};
// C++ derived class
class CppDerived : public Base
{
public:
virtual ~CppDerived() {}
std::string hello()
{
return "Hello from C++!";
}
};
// Familiar Boost.Python wrapper class for Base
class BaseWrap : public Base
{
public:
BaseWrap(PyObject* self_)
: self(self_) {}
std::string hello() { return python::call_method<std::string>(self, "hello"); }
PyObject* self;
};
// Pack the Base class wrapper into a module
BOOST_PYTHON_MODULE(embedded_hello)
{
python::class_<Base, BaseWrap, boost::noncopyable>("Base")
;
}
void test()
{
//- INITIALIZATION -----------------------------------------------------------//
PyObject * mod;
// Register the module with the interpreter
if (PyImport_AppendInittab("embedded_hello", initembedded_hello) == -1)
throw std::runtime_error("Failed to add embedded_hello to the interpreter's "
"builtin modules");
// Initialize the interpreter
Py_Initialize();
mod = PyImport_ImportModule("Phello");
// Retrieve the main modules namespace
python::handle<> mod_namespace(
python::borrowed(PyModule_GetDict(mod)));
// Extract the raw Python object representing the just defined derived class
python::handle<> class_ptr(
PyRun_String("PythonDerived\n", Py_eval_input,
mod_namespace.get(), mod_namespace.get()) );
// Wrap the raw Python object in a Boost.Python object
python::object PythonDerived(class_ptr);
//- MAIN PROGRAM -------------------------------------------------------------//
// Creating and using instances of the C++ class is as easy as always.
CppDerived cpp;
std::cout << cpp.hello() << std::endl;
// But now creating and using instances of the Python class is almost
// as easy!
python::object py_base = PythonDerived();
Base& py = python::extract<Base&>(py_base)();
std::cout << py.hello() << std::endl; //<<=== ERROR From here when freeing py.hello() result string
}
int main()
{
if (python::handle_exception(test))
{
if (PyErr_Occurred())
PyErr_Print();
return 1;
}
return 0;
}
// Phello.py
from embedded_hello import *
class PythonDerived(Base):
def hello(self):
return 'Hello from Python!'
PObj = PythonDerived()
print PObj.hello()
// Result
Hello from Python!
Hello from C++!
Hello from Python!
// and error!
Debug Assertion Failed!
Program : C:\\xxxxx
File: dbgheap.c
Line : 1044
Expression: _CrtlsValidHeapPointer(pUserData)
......
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk