views:

553

answers:

2

Or, what is the opposite of +(void)initialize?

Here's my situation:

I have a class Unit, whose -(id)initWithName: function takes data from a global NSDictionary, which is created lazily, defined in the Unit.m file as:

static NSMutableDictionary *unitLibrary = nil;

Where do I call [unitLibrary release]?

+7  A: 

You can call it at a location in which you know the dictionary is not needed anymore. If it is needed throughout the entire lifecycle of the application, then you don't have to do anything as all memories will be reclaimed by the OS when the app terminates.

Boon
This is on the iphone, so there can't be any memory leaks. When the user hits the home button and exits the app, my entire heap is deallocated?
Eric Maxey
Yes, when they hit home your entire address space is disposed of, including your heap.
Louis Gerbarg
No need to do spring cleaning just before you demolish the house. Terminating the app is release enough, and faster.
PeyloW
A: 

There's no general-purpose answer. You should deallocate it when you're sure it won't be used again. Possible candidates might be in the applicationWillTerminate delegate message, or through an atexit() function.

Mark Bessey
There's no point in releasing objects when the application terminates — the memory is just about to be freed anyway.
Chuck
Yes, this is bad - just let the OS clean it up. There's no point to doing work here. You're just delaying app quit.
Ken
It's arguably useful, to reduce the amount of clutter produced by leak-detecting tools. But probably not worth the effort.
Mark Bessey