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

abtree
  • 325
  • 4
  • 10

2 Answers2

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.

Boon
  • 40,656
  • 60
  • 209
  • 315
  • 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? – abtree Oct 06 '09 at 23:53
  • 1
    Yes, when they hit home your entire address space is disposed of, including your heap. – Louis Gerbarg Oct 06 '09 at 23:55
  • 2
    No need to do spring cleaning just before you demolish the house. Terminating the app is release enough, and faster. – PeyloW Oct 07 '09 at 18:46
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.

Mark Bessey
  • 19,598
  • 4
  • 47
  • 69
  • There's no point in releasing objects when the application terminates — the memory is just about to be freed anyway. – Chuck Oct 07 '09 at 00:45
  • 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 Oct 07 '09 at 01:01
  • 1
    It's arguably useful, to reduce the amount of clutter produced by leak-detecting tools. But probably not worth the effort. – Mark Bessey Oct 07 '09 at 01:39