[C++-sig] returning pointer to instance and object ownership

Max Ivanov ivanov.maxim at gmail.com
Thu Aug 21 21:51:09 CEST 2008


Hi all! Some newbie questions again.

class B {}

class A {
  B* add();
 }

method "add" of class A creates a new instance of class B and stores
reference to it somewhere inside.

I'm trying to expose it to python this way:
".def("add", &A::add, bp::return_value_policy<bp::manage_new_object>())"

It works ok. If I change returned B instance I'm able to see changes
accessing them from A instance - I hope that means that returned
pointer and one stored inside A are related to same instance. Problems
accour when I try to delete objects:
a = A()
b = a.add()
del b
del a
Segmentation fault.

It's the simpliest case I could imagine, since add() method should
always return new object, but still couldn't understand how to do it
right.

ps. I've no control over A and B sources.



More information about the Cplusplus-sig mailing list