Objective C - Where do you dealloc global static variables?
Asked Answered
G

2

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

Glim answered 6/10, 2009 at 23:38 Comment(0)
V
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.

Vesta answered 6/10, 2009 at 23:50 Comment(3)
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?Glim
Yes, when they hit home your entire address space is disposed of, including your heap.Disepalous
No need to do spring cleaning just before you demolish the house. Terminating the app is release enough, and faster.Twosided
S
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.

Sociable answered 7/10, 2009 at 0:38 Comment(3)
There's no point in releasing objects when the application terminates — the memory is just about to be freed anyway.Sheri
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.Leiker
It's arguably useful, to reduce the amount of clutter produced by leak-detecting tools. But probably not worth the effort.Sociable

© 2022 - 2024 — McMap. All rights reserved.