Borkware Miniblog

July 14, 2010

QFF: NSDictionary and Autorelease

Filed under: programming, Questions From Friends — Mark Dalrymple @ 2:36 pm

I occasionally get Questions From Friends over mail or AIM or IRC.  Usually they’re simple things and quick to answer.  I figured I’d put the answers here too in case anyone’s really bored on a rainy sunday afternoon.

besides autoreleasing the memory, what is the difference between the following two statements?

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];

Nothing really.

why would I ever NOT want to autorelease? Seems like the 2nd statement is easier

If you’re going to be immediately hanging on to it, the autorelease is wasted work,
or if you’re doing it inside of a tight loop – it’s better to get rid of it immediately rather than let a bunch of stuff accumulate in an autorelease pool. Of course, you can make and drain your own pool inside of your loop. Autorelease pools are pretty fast. But if you have a big spike of memory, say by accumulated autoreleased objects waiting for the grim reaper, that’ll push out things like mapped-in code segments, which may need to be brought back in later on. And on the phone, cycles count again, so it’s a hair more efficient to avoid autorelease. Just make sure you don’t accidentally forget to release it when you’re done.

Advertisements

2 Comments »

  1. RE: why would I ever NOT want to autorelease? – You didn’t cover the other side of the question:

    What can go wrong if I don’t autorelease, and manually release later?

    When you separate the release from the creation, as Apple does in its examples, you pave the way for two bad outcomes:

    1.) Since the release is separate from the creation, you have to be more careful when you maintain the code: make sure the release still happens, and make sure it happens exactly once on all paths of execution through the code.

    2.) If some intervening code calls something that calls something that raises an NSException, the code you thought would release will never be called. Maybe I’ve spent too much time in C++, but it bugs me to write exception-unsafe code.

    If you’ve autoreleased, even on an autorelease pool you’ve created locally, then even if the exception throws past the creation of the pool, the pool will get drained anyway when a higher pool up the stack is drained. That’s the Obj-C exception-safety guarantee to make up for the fact that Obj-C lacks C++’s destructors.

    Comment by DavidPhillipOster — March 29, 2011 @ 1:29 pm

  2. He didn’t ask that question :-) And of course your points are 100% accurate. Thanks!

    Comment by Mark Dalrymple — March 29, 2011 @ 1:32 pm


RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.