[C++-sig] Re: (no subject)
David Abrahams
dave at boost-consulting.com
Mon Sep 29 01:24:11 CEST 2003
"Andrew Ellem" <alellem at uwaterloo.ca> writes:
> 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));
Andrew,
Clearly the error must be coming from somewhere else, for example
a destructor of one of your objects which uses a HANDLE. None of the
Boost.Python code manipulates any type called HANDLE.
> It seems that Boost is trying to delete the Battle class that is being
> passed to it. Is this correct?
It's impossible to tell much of anything from your example. Too much
information is missing. Can you post a small reproducible case?
> 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.
An __init__ function with a return value policy?? What is it
returning?
> Is this even what I want to do?
Doesn't seem to be.
> 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.
Yes, it seems like you're taking shots in the dark. Have you tried
reading the documentation on Call Policies and/or stepping through the
code that's crashing with a debugger?
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Cplusplus-sig
mailing list