title: Imlementation of Singleton Class
body: <p>i have been using mostly the appDelegate class for global variables but recently i came to know that its not a gud way for keeping global variables so i am trying to make a singleton class as following</p>
  
  <pre><code>@implementation globalVar
  
  
  static globalVar *_sharedInstance =nil;
  
  @synthesize totalTime;
  - (id) init
  {
      if (self = [super init])
      {
  
  
      }
  return self;
  }
  
  + (globalVar *) sharedInstance
  {
      @synchronized (self) {
          if (_sharedInstance == nil) {
              [[self alloc] init];
          }
      }
  
      return _sharedInstance;
  }
  + (id)allocWithZone:(NSZone *)zone {
      @synchronized(self) {
          if (_sharedInstance == nil) {
              _sharedInstance = [super allocWithZone:zone];
              return _sharedInstance;
          }
      }
      return nil;
  }
  - (id)copyWithZone:(NSZone *)zone
  {
      return self;
  }
  -(NSUInteger)retainCount
  {
      return NSUIntegerMax;  //denotes an object that cannot be released
  }
  - (void)release
  {
      // do nothing
  }
  
  - (id)autorelease
  
  {
      return self;
  }
  
  
  - (void) setTotalTime:(NSString *)time
  {
  
      @synchronized(self) {
          if (totalTime != time) {
              [totalTime release];
              totalTime = [NSString stringWithFormat:@"%@",time];
          }
      }
  
      //NSLog(@"time %@",totalTime);
  }
  -(NSString *)getTotalTime
  {
      @synchronized(self) {
          //NSLog(@"total %@",totalTime);
          return totalTime;
      }
  }
  </code></pre>
  
  <p>when i set the value for totaltime in my appDelegate class and retrieve it in that class only i get the correct value. but when i only retrieve the value in some other class i get BAD EXCESS. i first create the sharedinstance and then only call this method then why am i getting this error??</p>
  
  <pre><code>globalVar *myEngine = [globalVar sharedInstance];
  
  NSLog(@"about %@",[myEngine totalTime]);
  </code></pre>
  
  <p>in my app delegate</p>
  
  <pre><code>globalVar *myEngine = [globalVar sharedInstance];
  
  [myEngine setTotalTime:totalTime];
  
  NSLog(@"in app delegate%@",[myEngine getTotalTime]);
  </code></pre>