On Sep 16, 2008, at 7:59 PM, Dave DeLong wrote:

The general rule with convenience class methods like that is that they return an autoreleased object.

They return an object for which you do not have responsibility to release. It may or may not be technically autoreleased.

What that means is that unless you retain it, it will disappear at some time in the future (whenever the current AutoreleasePool gets drained).

It _may_ disappear, not "will".


So if you want to "reclaim" the space, you don't have to do anything. If you want to reclaim it immediately, I think you ought to be able to do:

NSString * str = [NSMutableString string];
//do stuff with str
[[str retain] release];

Nope. The retain/release pair has no effect. They cancel each other out.

HOWEVER, that might cause funky things to happen with the autorelease pool.

The above has _no_ effect on the autorelease pool. An autorelease pool is just a collection of object pointers to which 'release' will be sent at some later time.

Objects are added to the pool over time but never removed from the pool until the pool drains itself. Furthermore, the pool doesn't care what the retain count of objects in it are.

You seem to be imagining a system where 'retain' accomplishes its work by removing the object from the autorelease pool. It doesn't. retain just increments the object's reference count.

So the best idea is to do nothing and let the autorelease pool take care of it.

Unless you create the object with either +alloc, -copy, - copyWithZone:, -mutableCopy, or -mutableCopyWithZone:, then you can't arrange for it to be released immediately. That's because you are not necessarily the sole owner. Even if you do create it with one of those methods, if you pass the object to anything else, it might modify the object's lifetime with -retain. So, there are very limited circumstances under which you can definitively force an object's memory to be reclaimed on your schedule. And generally, you don't need that sort of control.

If you obtained an object from a convenience constructor, you don't need to do anything to allow it to be reclaimed. You must not release it (except to balance any explicit retain).

Remember, in Cocoa memory management, you are only responsible for the objects you directly create or retain. Objects which are indirectly created for you (for example, by convenience constructors) are not your responsibility. Just take care of the things you need to and don't concern yourself with the things you don't need to.

Cheers,
Ken

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to