Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [OT] Retain count riddle



On Mar 31, 2004, at 1:46 PM, Bob Smith wrote:

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?

The simplest and best thing to do is to follow the memory management pattern... If you allocated the object (alloc/copy/new/etc) then you are responsible for releasing it (balance the implicit retain) and if you use an object past the current autorelease pool you need to retain it (or maintain the implicit retain) while you are using it. Hiding this work in a setter is the best way to go.

-Shawn
_______________________________________________
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.


References: 
 >Re: [OT] Retain count riddle (From: Christopher Behm <email@hidden>)
 >Re: [OT] Retain count riddle (From: Bob Smith <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.