[C++-sig] transfer ownership problem

zeb zebbey at gmail.com
Wed May 4 17:01:07 CEST 2011


hi, I have some problem about transfer-ownership. C++ code is like this:

class RefCounter {
   int _count;
public:
    RefCounter() : _count(1) {}
    virtual RefCounter() {}
    void addRef() { ++_count; }
    void release() { --_count; if (_count == 0) delete this; }
};

class A : public RefCounter {...};

class B {
public:
    void setA(A* a) {    // B takes the ownership of A.
        _a = a;
        a.addRef();
    }
private:
    A* _a;
};

So, in python, I use these classes like:
a = A() # the reference count of a is 1 now.
b = B()
b.setA(a) # B take the ownership of A, and now the reference count is 2.
a.release() # a should call release to decrease the reference count, but now
a is invalid. Python says: did not match C++ signature.

I can change the C++ code to solve this problem, but I really don't want to
change C++ code.
It's better to solve it by other way.

Is there any one could give me some advice?

THANK YOU VERY MUCH.




--
View this message in context: http://boost.2283326.n4.nabble.com/transfer-ownership-problem-tp3495846p3495846.html
Sent from the Python - c++-sig mailing list archive at Nabble.com.


More information about the Cplusplus-sig mailing list