RE: [OT] Retain count riddle
RE: [OT] Retain count riddle
- Subject: RE: [OT] Retain count riddle
- From: "Jonathan E. Jackel" <email@hidden>
- Date: Wed, 31 Mar 2004 17:34:53 -0500
>
This discussion raises a question I'd like to pose. (I'm relatively
>
new to Cocoa and Objective-C, so apologies if this is a dumb question
>
or one already answered a hundred times.) In this example the macro
>
does not work because [path lastPathComponent] returns a newly created
>
and autoreleased object, so each time it is invoked you get a different
>
object. However, if that method returned a weak reference to an
>
object, such as methods like [mySubView window] do, the macro would
>
have worked fine because the returned object would always be the same
>
one. How do I know which sort of object reference a particular method
>
is going to return? Likewise, in designing my own objects, are there
>
any rules I should follow about this sort of behavior?
>
>
Bob S.
Assume the worst. You do not need to know whether [path lastPathComponent]
returns a new object or a weak ref to the same object. Either way,
retaining it while you need it and releasing it when you are done will work.
So just do that.
As I'm sure many others will point out, if you don't use alloc/init, new, or
copy, you cannot assume the object will stick around beyond the scope of
your code. It may do so, but you can't count on it. Don't program as if
you can.
Jonathan
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.