what you propose is functionally identical to what is happening in XMLVM
right now. We had to add the returned reference to an autorelease pool
because we don't know if the caller consumes the reference or not. In
Java, you can just simply ignore the result.
In XMLVM, we have the following memory management rules:
- the caller owns input arguments. If the callee needs to save
the reference, it has to do a retain.
- the caller takes over ownership of the return argument.
When you write within the callee:
return [_op1.o autorelease];
_op1.o will be added to the autorelease pool of the caller. I guess
because of this asymmetry I decided back then to let the caller decide
what it wants to do. When the caller knows that it doesn't need the
reference, the caller can also call release instead of autorelease
(which is a much more expensive operation). But I agree that right now
we always add the return reference to the autorelease pool, so one might
as well do it in the callee. But in the future we might add this
optimization, so generally I would prefer to leave it the way it is.
Apart from the fact that it would be a lot of work to make that change:
all the hand-written Objective-C stubs would need to be changed accordingly.
Arno
On 12/13/09 6:14 PM, Gergely Kis wrote:
> Hi,
>
> I observed, the following pattern in the generated code (in pseudocode):
>
> _op1.o = [ref methodCall];
> [op1.o autorelease];
>
> However, the Cocoa memory management guide says, that you only need to
> release a reference, if you own it. It also says that you only own an
> object, if it was created with init, new or copy, or you have called
> retain on it.
>
> This means that in general if you call a method, you do not own the
> returned object, so you should not call autorelease on it.
> Why does this XMLVM the other way around? Is it because of the separate
> autoreleasepool for each method?
> It could generate the return sequence like this, and still follow the
> general rules of Cocoa memory management:
>
> in the returning method:
> [_op1.o retain];
> [_pool release];
> return [_op1.o autorelease];
>
> in the caller do not call autorelease.
>
> Am I missing something?
>
> Best Regards,
> Gergely
>
> --
> Kis Gergely
> MattaKis Consulting
> Email: ger...@ma... <mailto:ger...@ma...>
> Web: http://www.mattakis.com
> Phone: +36 70 408 1723
>
>
>
> ------------------------------------------------------------------------------
> Return on Information:
> Google Enterprise Search pays you back
> Get the facts.
> http://p.sf.net/sfu/google-dev2dev
>
>
>
> _______________________________________________
> xmlvm-users mailing list
> xml...@li...
> https://lists.sourceforge.net/lists/listinfo/xmlvm-users
|