<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2900.3059" name=GENERATOR></HEAD>
<BODY>
<DIV><SPAN class=562151315-02042007><FONT face=Arial
size=2>Hello,</FONT></SPAN></DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial size=2>I've got the
following class Foo, which is noncopyable, exposed to
Python:</FONT></SPAN></DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial size=2>
class Foo: ::boost::noncopyable<BR> {<BR>
public:<BR> ::boost::python::object
myself()<BR>
{<BR> return
::boost::python::object(::boost::python::ptr(this));<BR>
}<BR> };<BR> <BR>
::boost::python::class_< Foo, ::boost::noncopyable >
("Foo")<BR> .def("myself",
&Foo::myself)<BR> ;</FONT></SPAN></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial size=2>I create a Foo()
instance from Python:</FONT></SPAN></DIV>
<DIV><SPAN class=562151315-02042007> <FONT face=Arial
size=2>>>> f = Foo()</FONT></SPAN></DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial size=2>and now I want the
myself() method to return a Python object pointing to the same C++ object. With
the code above, I can do:</FONT></SPAN></DIV>
<DIV><SPAN class=562151315-02042007> <FONT face=Arial
size=2>>>> m = f.myself()</FONT></SPAN></DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial size=2>and use m as if it
were f (though f==m returns False, but that's not an issue for
me).</FONT></SPAN></DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial size=2>However, if I delete
f, or f goes out of scope, the C++ instance is deleted and m points to a deleted
C++ object.</FONT></SPAN></DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial size=2>If I replace the
return statement with </FONT></SPAN></DIV>
<DIV><SPAN class=562151315-02042007> <FONT face=Arial
size=2>return ::boost::python::object(this);</FONT></SPAN></DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial size=2>I get a runtime
error: "No to_python (by-value) converter found for C++ type: class
Foo",</FONT></SPAN></DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial size=2>which makes sense
because the class is noncopyable.</FONT></SPAN></DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial size=2>What is the correct
way to return the embedded Python object in the myself() method, such that
reference counting works, i.e., the C++ instance is deleted when both f and m
objects are deleted.</FONT></SPAN></DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial size=2>My actual use case
does not simply return 'this', but returns a list of class instance which hold a
pointer to the Foo instance as a data member.</FONT></SPAN></DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial size=2>Thanks for any
help,</FONT></SPAN></DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial size=2>Koen Van
Herck.</FONT></SPAN></DIV></BODY></HTML>