[C++-sig] runtime error R6025 - pure virtual function call
Dave Wolfe
dwolfe at gforcetech.com
Thu Jan 25 22:53:16 CET 2007
I'm being driven slowly mad by the following error:
C:\projects\PyScheduler\dtest.exe
In PyHelloPrinter.__init__()...
runtime error R6025
- pure virtual function call
The setup is: I've written the simplest C++ app I could manage that
embeds Python, and tries to create (in C++) an instance of a Python
class (defined in pyhello.py) that implements a C++-defined interface.
I've spent an embarrassing number of hours trying to figure this out. If
I don't find a solution soon, I'll be forced to conclude that I am
simply too dense to use Boost.Python. `:-}
I'm using ActiveState Python 2.4.3 on (32-bit) WinXP, and a snapshot of
boost downloaded from CVS a few days ago. Compiler is VC++ 7.1.
Everything works fine from Python; if I say: "python pyhello.py" it runs
with no problems. But I get 'runtime error R6025' without fail when I
try to run my C++ app.
The code I'm running, shorn of all adornments, is appended below. Any
help is *much* appreciated!
TIA,
- Dave W.
----------
// File: main.cpp
ICallable* CreatePythonHelloPrinter(std::string greetee)
{
ICallable* retval = NULL;
try
{
bpl::object main = bpl::import("__main__");
bpl::object global = main.attr("__dict__");
bpl::exec_file("pyhello.py", global, global);
bpl::object PyHelloPrinter = global["PyHelloPrinter"];
bpl::object myPyPrinter = PyHelloPrinter(greetee);
retval = bpl::extract<ICallable*>(myPyPrinter);
}
catch (bpl::error_already_set const&)
{
std::cerr << GetPythonException();
}
return retval;
}
int main()
{
// Init the python interpreter
Py_Initialize();
ICallable* pyGreeter = CreatePythonHelloPrinter("World");
if (pyGreeter) pyGreeter->operator()();
// Shut down python
Py_Finalize();
}
----------
# File: pyhello.py
from Callable import *
class PyHelloPrinter(ICallable):
def __init__(self, greetee):
ICallable.__init__(self)
self._name = greetee
def __call__(self):
print 'PyHelloPrinter(): Hello, %s!' % (self._name,)
----------
// File: Callable.h
#ifndef CALLABLE_H
#define CALLABLE_H
#include <boost/noncopyable.hpp>
struct ICallable
{
virtual ~ICallable() { }
virtual void operator()() = 0;
};
#endif // !CALLABLE_H
----------
// File: Callable.cpp
#include <Callable.h>
#include <boost/python.hpp>
using namespace boost::python;
struct PyWrap_ICallable : ICallable, wrapper<ICallable>
{
void operator()()
{
if (override func = get_override("__call__"))
func();
}
};
BOOST_PYTHON_MODULE(Callable)
{
class_<PyWrap_ICallable, boost::noncopyable>("ICallable")
.def("__call__", pure_virtual(&ICallable::operator() ) )
;
}
More information about the Cplusplus-sig
mailing list