Re: Some really simple release/autoRlease questions
Re: Some really simple release/autoRlease questions
- Subject: Re: Some really simple release/autoRlease questions
- From: Greg Titus <email@hidden>
- Date: Sat, 15 Mar 2003 14:36:21 -0800
On Saturday, March 15, 2003, at 02:17 PM, Krevnik wrote:
On Saturday, March 15, 2003, at 02:05 PM, Jeffrey Mattox wrote:
If I do this, theDictionary gets a retainCount of 1.:
NSMutableDictionary *theDictionary =
[NSMutableDictionary dictionaryWithCapacity:1];
1. Is it in the autoRelease pool? Is that the case with all class
methods that create and return objects?
Yes, and it is the normal behavior of all of Apple's class methods
that do that. 3rd Party classes may behave differently, but they
shouldn't.
This is correct.
2. If I have a big loop that uses an NSString in the loop, is it
considered good form (to conserve memory) to use alloc/init and then
release the string from the previous loop, thusly:
If memory is your priority, I would take this route... but if you need
speed, that is going to be slow as snot on a loop that iterates
thousands of times. The best (IMO) for a speedy loop is to create
objects in an autoreleased state and ignore the deallocation. That way
the cycles needed to collect the memory from them is used during the
run loop, rather than your tight loop.
But this is bad advice, IMHO. Autoreleasing an object is _by
definition_ more expensive than directly releasing it, because you have
the overhead of adding a pointer to the object to the autorelease
pool's list, and iterating over the list when the pool is freed, as
well as the actual release itself.
It doesn't matter whether that release happens in your tight loop, or
in AppKit's runloop. Either way, those cycles are taken before your
application can accept another event from the user. From the user's
point of view, it is all happening at the same time.
In short: use autorelease as a convenience. It is an extremely useful
convenience when returning objects, or within code that may cause
exceptions. But directly releasing an object, when possible, is nearly
always better.
3. I've looked as the utility ObjectAlloc, but it generates an
unwieldy output. Is that the best way to find leaks due to objects I
explicitly create? Is there a way to reduce the clutter?
I honestly don't know this one.
We found it unwieldy as well, which is why we wrote OmniObjectMeter.
You could try it out:
http://www.omnigroup.com/developer/omniobjectmeter/
Hope this helps,
-Greg
_______________________________________________
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.