[C++-sig] Ownership question.

Nicodemus nicodemus at globalite.com.br
Sun Apr 27 09:00:16 CEST 2003


tALSit de CoD wrote:

> I have a class method that takes ownership of the object passed into 
> it, and I want to know, how do I let boost.python know about this and 
> not to manage that object anymore?
>
> Should i do a kludge job like make a function that return a new 
> object, but telling boost.python not to manage it, then, when I add it 
> into the method, it's "disappeared" from boost.python?
>
> Original thing:
>
> class cAnimCurveKey { /* stuff */ }
>
> class cAnimCurve {
>         void addKey (cAnimCurveKey * key); // takes ownership of the key
> }
>
> When i wrap that in boost.python, and i create a key and then add it 
> to the animation curve:
>
> >>> aninCurve = cAnimCurve ()
> >>> key = cAninCurveKey ()
> >>> animCurve.addKey (key)
>
> What happens there?? Is python the owner of the object key? I was 
> thinking of this hack:
> (In C++ / boost.python)
>
> cAnimCurveKey * newAnimCurveKey () { return new cAnimCurveKey (); }
>
> BOOST_PYTHON_MODULE (kikura) {
>         def ("newAnimCurveKey", newAnimCurveKey, return_value_policy 
> <reference_existing_object> ());
> }
>
> And that way, python is not in charge of that object, and in theory, 
> every key that gets created, is created to be put in an AnimCurve, so 
> it's not _that_ bad, is it?
>
>
> OOOOORRRRRR  Is there a way to specify to boost.python that you don't 
> want it to manage or track that object anymore?
>
> Thanks folks
> Cheers!


Unfortunately, I don't think you will get a "nice" way to acomplish 
this, because of how garbage collection works in Python. Consider:

 >>> def foo(s):
...    # use s
...    del s  # useless, but just to illustrate the point
...   
 >>> x = [1, 2, 3]
 >>> foo(x)
 >>> x
[1, 2, 3]

So, there's simply no way for a Python function to delete a variable 
passed as parameter... actually, you can't delete any variable and be 
sure that it was actually release, because there could still be 
references to it somewhere else. In python, there's no ownership (you 
can have references with no ownership - weak references), just 
references to an object, that will be disposed when no more references 
exist.


Regards,
Nicodemus.

>
>
>   tALSit de CoD  |  talsit.org
>
>
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
>
> .
>






More information about the Cplusplus-sig mailing list