[C++-sig] Exposing a C++ function that takes 2 out arguments to python
Abhi
abhi at qualcomm.com
Wed May 10 03:00:23 CEST 2006
How do I expose a C++ function that takes 2 out arguments with different
memory ownership semantics to python. For instance,
consider A, B to be user-defined types
class C
{
public:
B* b_;
void foo(A*& a, B*& b)
{
// create A on the heap and let caller take ownership
A* a2 = new A()
a = a2;
// create B, but we retain ownership
b_ = new B();
b = b_;
}
~C()
{
delete b_;
}
};
Notice that object "a" is to be destroyed in python, while "b" is owned by
C++
How do I expose this in python?
I tried the following:
bp::tuple foo_Wrapper(C& self)
{
A* a;
B* b;
self.foo(a, b)
// Case1. In this case the "a" is never destroyed in python
// -- "a" is leaked -- NOT what I want!
// "b" is ok
make_tuple(bp::ptr(a), bp::ptr(b) );
// Case2: In this case they get copy constructed into the tuple
// -- that means memory for "a" is leaked!
// -- I don't really want a copy -- Changes the semantics, I want to
// modify "a" & "b" in python && it is expensive to copy
make_tuple(a, b);
}
Any other ideas?
thanks
-= Abhi
More information about the Cplusplus-sig
mailing list