开发者

Singletons in Objective-C for storing and accessing program-wide variables. A few questions

I am using Apple's MyGizmoClass Singleton class for program-wide "session variables" and loving it! However, when I run "Build and Analyze" it gives weird results. Maybe my usage is wrong (it works, but it may be working due to a flakey side effect). Here is an example.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    int ct = 0;
    MyGizmoClass *myGizmoClass= [MyGizmoClass sharedManager];
    ct = [[myGizmoClass searchResultsForResortLocation] count];
    [myGizmoClass release];
    NSLog(@"ct: %d",ct);
    return ct;
}

or

- (void)viewWillAppear:(BOOL)animated {


     // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
     //self.navigationItem.rightBarButtonItem = self.editButtonItem;

    NSMutableString *which_resort = [[NSMutableString alloc] init];
    NSMutableString *category_code = [[NSMutableString alloc] init];
    MyGizmoClass *myGizmoClass= [MyGizmoClass sharedManager];

    ...

    which_resort = [self which_resort_location_are_we_in];

    ... 
    [myGizmoClass setWhich_resort:which_resort];

    int useDebugMode = [myGizmoClass useDebugMode]; 

    ...


    [myGizmoClass release];

    [which_resort release];
    [category_code release];

    [super viewWillAppear:animated];

}

Again, this usage may be WAY off, but I thought each method I used a value from the singleton I had to do:

MyGizmoClass *myGizmoClass= [MyGizmoClass sharedManager];

and

[myGizmoClass release];

BUT i am getting these errors during Analyze:

/Users/jgobble/Documents/ProgramName/Classes/ResortsListViewController.m:495:2 Incorrect decrement of the reference count of an object is not owned at this point by the caller /Users/jgobble/Documents/ProgramName/Classes/ResortsListViewController.m:493:30 Method returns an Objective-C object with a +0 retain count (non-owning reference) /Users/jgobble/Documents/ProgramName/Classes/ResortsListViewController.m:495:2 Incorrect decrement of the reference count of an object is not owned at this point by the caller

Now, please take this into account: I am calling this:

MyGizmoClass *myGizmoClass= [MyGizmoClass sharedManager];
开发者_运维百科

at the beginning of EVERY method need a "session variable" and then calling:

[myGizmoClass release];

at the end of that method PRIOR to returning a result (if i return a result from that function.

Is this not the way I should do it?

This is the ONLY thing that the analyzer (thank goodness) is reporting wrong with the program. I do not know if i should ignore it. I do not know if I am doing the calls in the right place.

Here is another question I am worried about: Does this work or do the subsequent calls to *myGizmoClass mess anything up?

-(void) function_a {
     MyGizmoClass *myGizmoClass= [MyGizmoClass sharedManager];

     [myGizmoClass setC:1];

     int result_b = [self function_b];

     printf("Addition result is: %d", result_b);

     [myGizmoClass release]
}

-(int) function_b {
     MyGizmoClass *myGizmoClass= [MyGizmoClass sharedManager];

     int b = 0;

     b = b + [myGizmoClass c];

     [myGizmoClass release]

     return b;
}

(i have not tested the code above)

In other words there anything wrong with calling MyGizmoClass *myGizmoClass= [MyGizmoClass sharedManager]; from function_b when you have not released MyGizmoClass *myGizmoClass= [MyGizmoClass sharedManager]; from function_a?


The problem is the way that you are using a singleton. The general idea is that you don't retain or release any singleton objects, because there is only one instance that hangs around, and the Apple implementation of singletons are coded in such a way that you can't. This brings about some issues that Peter Hosey blogged about with a better implementation of a Cocoa singleton.

The warnings that the Analyzer logs are simply stating that you "shouldn't" retain or release the singleton object; by virtue of the fact that you call [MyGizmoClass sharedManager] you shouldn't be releasing anyway (following the Cocoa memory management guidelines).


You should not be releasing myGizmoClass. When [MyGizmoClass sharedManager] is called, the returned object is not being retained. Since it's a shared object, its only owner is its class other object is responsible for releasing it unless they first retain it.

Take a look at this short guide from Apple on using singleton objects in your Objective-C code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜