title: ios singleton class crashes my app
body: <p>I have a problem with an singleton pattern. </p>
  
  <p>I have read the following tutorials about singleton classes and have created my own.
  <a href="http://www.galloway.me.uk/utorials/singleton-classes/" rel="nofollow">http://www.galloway.me.uk/utorials/singleton-classes/</a>
  http://www.johnwordsworth.com/2010/04/iphone-code-snippet-the-singleton-pattern/</p>
  
  <p>The first time i build &amp; run the app it works like it should. No problems at all!
  But when i rebuild the app the singleton class does not work properly anymore. The first init works like it should but when i call it again after a button click it crashes my app.</p>
  
  <p>My singleton class: </p>
  
  <p>BPManager.h</p>
  
  <pre><code>@interface BPManager : NSObject {
      NSString *dbPath;
  }
  
  @property (nonatomic, retain) NSString *dbPath;
  
  + (id)bpManager;
  - (void)initDatabase:(NSString *)dbName;
  - (int)getQuestions;
  
  @end
  </code></pre>
  
  <p>BPManager.m</p>
  
  <pre><code>static BPManager *sharedMyManager = nil;
  
  @implementation BPManager
  
  @synthesize dbPath;
  
  - (void)initDatabase:(NSString *)dbName
  {   
      dbPath = dbName;
  }
  -(int)getQuestions
  {
      NSLog(@"getQuestions");
  }
  
  
  - (id)init {
      if ((self = [super init])) {
      }
      return self;
  }
  
  + (BPManager *) bpManager {
      @synchronized(self) {
  
          if(sharedMyManager != nil)  return sharedMyManager;
  
          static dispatch_once_t pred;        // Lock
          dispatch_once(&amp;pred, ^{             // This code is called at most once per app
              sharedMyManager = [[BPManager alloc] init];
          });
      }
  
      return sharedMyManager;
  }
  
  - (void)dealloc {
      [dbPath release];
      [super dealloc];
  }
  </code></pre>
  
  <p>When i call the following code when building my interface, the app creates the singleton:</p>
  
  <pre><code>BPManager *manager = [BPManager bpManager];
  [manager initDatabase:@"database.db"];
  </code></pre>
  
  <p>Note: At this point i can create references to the class from other files as well. But when i click on a button it seems to loose his references.  </p>
  
  <p>But when a button is clicked, the following code is ecexuted:</p>
  
  <pre><code>BPManager *manager = [BPManager bpManager];
  int count = [manager getQuestions];
  </code></pre>
  
  <p>The app should get the sharedInstance. That works, only the parameters (like dbPath) are not accessible. Why is that?</p>
  
  <h2>Edit:</h2>
  
  <p>after some research, i have changed the method to:</p>
  
  <pre><code>+ (BPManager *) bpManager {
      @synchronized(self) {
  
          if(sharedMyManager != nil)  return sharedMyManager;
  
          static dispatch_once_t pred;        // Lock
          dispatch_once(&amp;pred, ^{             // This code is called at most once per app
              sharedMyManager = [[BPManager alloc] init];
          });
      }
  
      return sharedMyManager;
  }
  </code></pre>
  
  <p>But the problem is not solved</p>