11

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]?

2 Answers 2

13

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
Yes, when they hit home your entire address space is disposed of, including your heap.
No need to do spring cleaning just before you demolish the house. Terminating the app is release enough, and faster.
3

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.

3 Comments

There's no point in releasing objects when the application terminates — the memory is just about to be freed anyway.
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.
It's arguably useful, to reduce the amount of clutter produced by leak-detecting tools. But probably not worth the effort.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.