Jeff
Jeff

Reputation: 27

Object C static variable memory question

As you know, we define a static variable parameter in Object c,

what I am concerning, how to map this static Object variable from memory management view.

for example

@interface classA:NSObject
+ (id) getInstance;
@end

static ClassA staticObject = nil;
@implementation ClassA
 + (id){
      if(staticObject)
         staticObject = [doinitStuff];
      return staticObject   
 }
@end

now the question is: who is responsible to hold staticObject memory, when it will be released.

what I am thinking:

Runtime OS will monitor application, and only when application really exit, this memory will be released. from RUNtime view, it is retained and count = 1;//just 1

any thought would be great appreciated. Thanks.

Upvotes: 1

Views: 784

Answers (1)

bbum
bbum

Reputation: 162712

It is no different than any other variable. If you want it to stick around, retain it. If not, don't (or, if retained, release/autorelease it).

There is non need to release memory on exit of an application. When your app terminates, all resources will be reaped regardless.

Upvotes: 2

Related Questions