[C++-sig] (no subject)

Andrew Ellem alellem at uwaterloo.ca
Sun Sep 28 23:21:25 CEST 2003


I am having problems with Boost.Python (attempting to) delete classes
passed to it.

This is being done in the Battle constructor (which is invoked from
C++).
I'm basing my work off the embedding.cpp example, and I am creating the
Python-derived class like this:  (Where Script is a .py file and
Script.Standard is derived from the Script in the Battle module below).

[...]
handle<> class_ptr( 
    PyRun_String("Script.Standard\n", Py_eval_input, 
       main_namespace.get(), main_namespace.get()) );
    
object PythonDerived(class_ptr);

object py_base = PythonDerived(this);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This line is the problem
[...]


My module is defined like this:

BOOST_PYTHON_MODULE(Battle)
{  
    class_<Battle>("Battle", no_init)
        .def("GetHandle", &Battle::GetHandle)
        ;
    
    class_<Script, ScriptWrapper, boost::noncopyable>
        ("Script", init<Battle*>())
        ;
}


The PythonDerived constructor is called successfully (it prints, etc).
However in the PythonDerived(this) call, I get error about an invalid
HANDLE, in the Boost.Python instance_dealloc function at the line 284 of
class.cpp:

    instance_holder::deallocate(inst, dynamic_cast<void*>(p));

It seems that Boost is trying to delete the Battle class that is being
passed to it.  Is this correct?

How do I prevent this?  I tried adding
return_value_policy<reference_existing_object>() to the init<> of
Script, but it didn't' seem to change anything.  Is this even what I
want to do?

I also tried to add boost::noncopyable to my definition of Battle, in
hopes that it would prevent this, but then I would get errors about no
to_python converter.  I guess that I'm a little confused as to what
exactly is going on, and how to go about fixing it.



Thanks,
Andrew





More information about the Cplusplus-sig mailing list