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

  • SEARCH
  • Home
  • 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 6372511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:14:55+00:00 2026-05-25T01:14:55+00:00

I have a singleton called SettingsManager in my app that should take care of

  • 0

I have a singleton called SettingsManager in my app that should take care of reading/writing settings to a plist. Singleton is synthesized using macro from Cocoa With Love (zipped macro file). I have only changed retain’s return type to (oneway void) from (void). This is due to new compiler in iOS that can take care of memory management. My problem is that whenever i call savePrefs, button calling the selector “hangs” on active state, and sometimes I am getting EXC_BAD_ACCESS. So I guess I failed with memory management. Surprisingly, when no plist file is available (right after app installation), all the defaults are correctly stored and retrieved from my NSMutableDictionary. Below is my code. Will be super grateful for your help.

Martin

CODE:

1/ Singleton header

#import <Foundation/Foundation.h>

#define SALARY_PREF_KEY @"MonthlySalary"
#define DEFAULT_SALARY [NSNumber numberWithDouble: 0.0]
#define CURRENCY_PREF_KEY @"Currency"
#define DEFAULT_CURRENCY @"USD"
#define EXPENSES_PREF_KEY @"Expenses"
#define DEFAULT_EXPENSES [NSNumber numberWithDouble: 0.0]
#define TIME_INTERVAL_PREF_KEY @"TimeInterval"
#define DEFAULT_INTERVAL [NSNumber numberWithInteger: 2]
#define SAVINGS_PREF_KEY @"Savings"
#define DEFAULT_SAVINGS [NSNumber numberWithDouble: 0.0]

@interface SettingsManager : NSObject {
    NSString *prefsFilePath;
    NSMutableDictionary *settingsDictionary;
    NSNumber *salary;
    NSString *currency;
    NSNumber *expenses;
    NSNumber *timeInterval;
    NSNumber *savings;
}

+ (SettingsManager *)sharedSettingsManager;

@property (nonatomic, retain) NSString *prefsFilePath;
@property (nonatomic, retain) NSMutableDictionary *settingsDictionary;
@property (nonatomic, retain) NSNumber *salary;
@property (nonatomic, retain) NSString *currency;
@property (nonatomic, retain) NSNumber *expenses;
@property (nonatomic, retain) NSNumber *timeInterval;
@property (nonatomic, retain) NSNumber *savings;

    - (void) savePrefs;
@end

2/ Implementation:

#import "SynthesizeSingleton.h"
#import "SettingsManager.h"

@implementation SettingsManager

SYNTHESIZE_SINGLETON_FOR_CLASS(SettingsManager);

@synthesize settingsDictionary, 
            salary, 
            currency, 
            expenses, 
            timeInterval, 
            savings, 
            prefsFilePath;

- (id)init {
    self = [super init];  
    if (self) {
        if (prefsFilePath == nil) {
            NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
            prefsFilePath = [documentsDirectory stringByAppendingPathComponent:@"iEarn.plist"];
        }
        if ([[NSFileManager defaultManager] fileExistsAtPath: prefsFilePath]) {
            settingsDictionary = [[NSMutableDictionary alloc] 
                                  initWithContentsOfFile: prefsFilePath];
        } 
        else {
            settingsDictionary = [[NSMutableDictionary alloc] initWithCapacity: 7];
            [settingsDictionary setObject: DEFAULT_SALARY forKey: SALARY_PREF_KEY];
            [settingsDictionary setObject: DEFAULT_CURRENCY forKey: CURRENCY_PREF_KEY];
            [settingsDictionary setObject: DEFAULT_EXPENSES forKey: EXPENSES_PREF_KEY];
            [settingsDictionary setObject: DEFAULT_INTERVAL forKey: TIME_INTERVAL_PREF_KEY];
            [settingsDictionary setObject: DEFAULT_SAVINGS forKey: SAVINGS_PREF_KEY];
            [settingsDictionary writeToFile: prefsFilePath atomically: YES];
        }
        self.salary = [settingsDictionary objectForKey:SALARY_PREF_KEY];
        self.currency = [settingsDictionary objectForKey:CURRENCY_PREF_KEY];
        self.expenses = [settingsDictionary objectForKey:EXPENSES_PREF_KEY];
        self.timeInterval = [settingsDictionary objectForKey:TIME_INTERVAL_PREF_KEY];
        self.savings = [settingsDictionary objectForKey:SAVINGS_PREF_KEY];
    }
    return self;
}

- (void) savePrefs {
    [settingsDictionary setObject: salary forKey: SALARY_PREF_KEY];
    [settingsDictionary setObject: currency forKey: CURRENCY_PREF_KEY];
    [settingsDictionary setObject: expenses forKey: EXPENSES_PREF_KEY];
    [settingsDictionary setObject: timeInterval forKey: TIME_INTERVAL_PREF_KEY];
    [settingsDictionary setObject: savings forKey: SAVINGS_PREF_KEY];
    [settingsDictionary writeToFile: prefsFilePath atomically: YES];   
}

- (void) dealloc {
    [settingsDictionary dealloc];
    [super dealloc];
}
@end

3/ How I call savePrefs

- (IBAction)saveButtonPressed:(id)sender {
    [[SettingsManager sharedSettingsManager] setSalary: [NSNumber numberWithDouble: [salary.text doubleValue]]];
    [[SettingsManager sharedSettingsManager] setCurrency: currency.text];
    [[SettingsManager sharedSettingsManager] setExpenses: [NSNumber numberWithDouble: [expenses.text doubleValue]]];
    [[SettingsManager sharedSettingsManager] setTimeInterval: [NSNumber numberWithInt: [intervalStepper value]]];
    [[SettingsManager sharedSettingsManager] setSavings: [NSNumber numberWithDouble: [savings.text doubleValue]]];
    [[SettingsManager sharedSettingsManager] savePrefs];
}
  • 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-25T01:14:55+00:00Added an answer on May 25, 2026 at 1:14 am

    It seems that you access directly the prefsFilePath instance variable instead of using its accessor:

    prefsFilePath = [documentsDirectory stringByAppendingPathComponent:@"iEarn.plist"];
    

    The value stored is auto-released, so after the current pool is drained, the reference is no longer valid. Instead, you should use:

    self.prefsFilePath = [documentsDirectory stringByAppendingPathComponent:@"iEarn.plist"];
    

    or

    prefsFilePath = [[documentsDirectory stringByAppendingPathComponent:@"iEarn.plist"] retain];
    

    Note: You may either prefix all the property accesses to avoid problems, or rename the instance variables.

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

Sidebar

Related Questions

The goal is to have a singleton data controller class called FetchData.h/.m that pulls
I have a singleton class called Manager that holds a list of object instances:
I have a singleton that holds a lot of information on my App (ACCU.class).
I have a singleton called Singleton that manages certain variables needed across the application
I have a singleton object called PoolManager that loads and saves some data in
My app has a singleton class called CycleManager. I have created a sealed class
I have an asynchronous method in my EJB singleton that's called from another method
I have a singleton that uses the static readonly T Instance = new T();
I have a Singleton that is accessed in my class via a static property
I have a singleton object that use another object (not singleton), to require some

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.