<!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>&nbsp;</DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial size=2>I've got the 
following class Foo, which is noncopyable,&nbsp;exposed to 
Python:</FONT></SPAN></DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial 
size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; 
class Foo: ::boost::noncopyable<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp; 
public:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ::boost::python::object 
myself()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 
::boost::python::object(::boost::python::ptr(this));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
}<BR>&nbsp;&nbsp;&nbsp; };<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; 
::boost::python::class_&lt; Foo, ::boost::noncopyable &gt; 
("Foo")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .def("myself", 
&amp;Foo::myself)<BR>&nbsp;&nbsp;&nbsp; ;</FONT></SPAN></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</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>&nbsp;&nbsp;&nbsp; <FONT face=Arial 
size=2>&gt;&gt;&gt; f = Foo()</FONT></SPAN></DIV>
<DIV><SPAN class=562151315-02042007><FONT face=Arial 
size=2></FONT></SPAN>&nbsp;</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>&nbsp;&nbsp;&nbsp; <FONT face=Arial 
size=2>&gt;&gt;&gt; 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>&nbsp;</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>&nbsp;&nbsp;&nbsp; <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>&nbsp;</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>&nbsp;</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>