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 7530929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:02:52+00:00 2026-05-30T05:02:52+00:00

I’ve been stuck on this one for a while and any advice would be

  • 0

I’ve been stuck on this one for a while and any advice would be greatly appreciated. I’ve created a singleton object called SharedUser that represents the user who is currently using the app. This SharedUser object has two NSStrings (for username and userId) and an NSArray (for the company departments to which the user belongs). The user is initialized upon login with a method called initWithUsername. Inside this method the username is used to parse the user and get the user’s unique id number and the the user’s departments.

My problem is as follows: when I get the user through the sharedUser method at a later point in the app, the userId string is empty and causes an exc_bad_access error when I try to use it. I know that the userId string is being initialized though because I have observed this happening in the debugger. However, strangely enough, the username string still exists for the same object that is lacking the userId string. I’m very confused as to how the memory behind userId could be released while username would still hang around.

My SharedUser class looks like this:

@implementation SharedUser
@synthesize userId;
@synthesize username;
@synthesize departmentIds;

static SharedUser *sharedUser = nil;

+(SharedUser *)sharedUser {

@synchronized(self) {
        if (!sharedUser) {
            sharedUser=[[self alloc] init];
        }
    }
    return sharedUser;
}

+ (id)allocWithZone:(NSZone *)zone {
    @synchronized(self) {
        if (sharedUser == nil) {
             sharedUser = [super allocWithZone:zone];
             return sharedUser;
        }
    }

    return nil;
}

- (id)copyWithZone:(NSZone *)zone {
    return self;
}


- (id)retain {
    return self;
}


- (unsigned)retainCount {
    return NSUIntegerMax;
}


- (oneway void)release {
    // never release
}


- (id)autorelease {
    //do nothing
}


- (id) init {
    @synchronized(self) {
        [super init];
        username = @"";
        userId = @"";
        departmentIds = nil;
        return self;
    }
}


- (void) initWithUserName:(NSString *) loginName {

    username = loginName;

    //create a request and a connection    
    NSURL *url = [NSURL URLWithString:@"http://blah.com/url/for/the/json/"];   
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request startSynchronous];
    NSError *error = [request error];

    if (!error) { 
        NSString *responseString = [request responseString];
        NSArray *listOfPeopleDictionaries = [responseString JSONValue];

        for (NSDictionary *personDictionary in listOfPeopleDictionaries) {
             if ([loginName isEqualToString:[personDictionary  objectForKey:@"username"]]) {
                // Set the user id
                userId = [personDictionary objectForKey:@"id"];

                // Set the user's department
                NSArray *departmentsArray = [personDictionary objectForKey:@"organizations"];
                for (NSDictionary *department in departmentsArray) {
                    [departmentIds addObject:[department objectForKey:@"id"]];
                }
             }
         }
     }
     else {
        //give error
     }
 }

- (void)dealloc {
    // Should never be called
    [username release];
    [userId release];
    [departmentIds release];
    [super dealloc];
}

The following code would cause an exc_bad_access error on the third line.

SharedUser *user = [SharedUser sharedUser];
NSLog(@"%@", user.username);
NSLog(@"%@", user.userId);

That said, my question is where is the memory behind the userId string being released and what do I do to fix it? I’m brand new to stack overflow so please be gentle.

  • 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-30T05:02:53+00:00Added an answer on May 30, 2026 at 5:02 am

    userId is not being retained by your singleton. The value is owned by personDictionary, which is owned by listOfPeopleDictionaries, which is an autoreleased return value from [responseString JSONValue]. When the autorelease pool is purged the whole chain is released including userId.

    The solution is to retain userId, either by doing

    userId = [[personDictionary objectForKey:@"id"] retain];
    

    or

    self.userId = [personDictionary objectForKey:@"id"]
    

    username has the same issue, but probably isn’t crashing as you’re holding on to it in the object which is calling initWithUserName:.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string

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.