title: Singletons in Objective-C for storing and accessing program-wide variables. A few questions
body: <p>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.</p>
  
  <pre><code>- (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;
  }
  </code></pre>
  
  <p>or </p>
  
  <pre><code>- (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];
  
  }
  </code></pre>
  
  <p>Again, this usage may be WAY off, but I thought each method I used a value from the singleton I had to do: </p>
  
  <pre><code>MyGizmoClass *myGizmoClass= [MyGizmoClass sharedManager];
  </code></pre>
  
  <p>and</p>
  
  <pre><code>[myGizmoClass release];
  </code></pre>
  
  <p>BUT i am getting these errors during Analyze:</p>
  
  <blockquote>
    <p>/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</p>
  </blockquote>
  
  <p>Now, please take this into account:  I am calling this:</p>
  
  <pre><code>MyGizmoClass *myGizmoClass= [MyGizmoClass sharedManager];
  </code></pre>
  
  <p>at the beginning of EVERY method need a "session variable" and then calling:</p>
  
  <pre><code>[myGizmoClass release];
  </code></pre>
  
  <p>at the end of that method PRIOR to returning a result (if i return a result from that function. </p>
  
  <p>Is this not the way I should do it?</p>
  
  <p>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.  </p>
  
  <p>Here is another question I am worried about:  Does this work or do the subsequent calls to *myGizmoClass mess anything up?</p>
  
  <pre><code>-(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;
  }
  </code></pre>
  
  <p>(i have not tested the code above) </p>
  
  <p>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?</p>