Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6627479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:01:10+00:00 2026-05-25T22:01:10+00:00

I have a problem with an singleton pattern. I have read the following tutorials

  • 0

I have a problem with an singleton pattern.

I have read the following tutorials about singleton classes and have created my own.
http://www.galloway.me.uk/utorials/singleton-classes/
http://www.johnwordsworth.com/2010/04/iphone-code-snippet-the-singleton-pattern/

The first time i build & 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.

My singleton class:

BPManager.h

@interface BPManager : NSObject {
    NSString *dbPath;
}

@property (nonatomic, retain) NSString *dbPath;

+ (id)bpManager;
- (void)initDatabase:(NSString *)dbName;
- (int)getQuestions;

@end

BPManager.m

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(&pred, ^{             // This code is called at most once per app
            sharedMyManager = [[BPManager alloc] init];
        });
    }

    return sharedMyManager;
}

- (void)dealloc {
    [dbPath release];
    [super dealloc];
}

When i call the following code when building my interface, the app creates the singleton:

BPManager *manager = [BPManager bpManager];
[manager initDatabase:@"database.db"];

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.

But when a button is clicked, the following code is ecexuted:

BPManager *manager = [BPManager bpManager];
int count = [manager getQuestions];

The app should get the sharedInstance. That works, only the parameters (like dbPath) are not accessible. Why is that?

Edit:

after some research, i have changed the method to:

+ (BPManager *) bpManager {
    @synchronized(self) {

        if(sharedMyManager != nil)  return sharedMyManager;

        static dispatch_once_t pred;        // Lock
        dispatch_once(&pred, ^{             // This code is called at most once per app
            sharedMyManager = [[BPManager alloc] init];
        });
    }

    return sharedMyManager;
}

But the problem is not solved

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:01:11+00:00Added an answer on May 25, 2026 at 10:01 pm

    How about

    @interface BPManager : NSObject
    @property (nonatomic, copy) NSString *dbName;
    @property (nonatomic, assign) int questions;
    -(id) initWithDBName:(NSString*) dbName {
    @end
    
    #import "BPManager.h"
    @implementation BPManager
    @synthesize dbName=_dbName, questions;
    +(BPManager *)singleton {
        static dispatch_once_t pred;
        static BPManager *shared = nil;
        dispatch_once(&pred, ^{
            shared = [[BPManager alloc] initWithDBName:@"database.db"];
        });
        return shared;
    }
    -(id) initWithDBName:(NSString*) dbName {
        self = [super init]
        if (self) self.dbName = dbName;
        return self;
    }
    -(void)dealloc {   
        [_dbName release];
        [super dealloc];
    }
    @end
    
    BPManager *manager = [BPManager singleton];
    int count = [manager questions];
    

    The static is private to the implementation file but no reason it should be even accessible outside the singleton method. The init overrides the default implementation with the default implementation so it’s useless. In Objective-C you name the getter with the var name (count), not getCount. Initializing a class twice causes an undefined behaviour. No need to synchronize or check for if==nil when you are already using dispatch_once, see Care and Feeding of Singletons. NSString should always use copy instead retain in @property. You don’t need the dealloc because this is going to be active forever while your app is running, but it’s just there in case you want to use this class as a non singleton . And you probably are as good with this class being an ivar in your delegate instead a singleton, but you can have it both ways.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue with the static method in ASP.NET. I created the Singleton
I have the following classes defined: public interface Thingy { ... } public class
hi I created a view based application and I have three classes: -MyAppViewController; -Item
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with return statment >.< I want to store all magazine names
I have problem with starting processes in impersonated context in ASP.NET 2.0. I am
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem with ActionLink. I'd like to pass to my ActionLink parameter for
I have problem when I try insert some data to Informix TEXT column via
I do not have problem as such but I am quite new to Ruby.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.