1

I have a crash problem when I am trying to add object into array, I think I have problem with the way i create object and release it. But I am not quite sure cause I am still kinda weak with memory management

 NSMutableDictionary *schools = [[NSMutableDictionary alloc] init];
    [schools setObject:name forKey:kFavoriteSchoolName];
    //load data is getting data from NSUserDefault which I save
    NSMutableArray *loadedArray = [self loadData];

//if loadedarray has object in there, then continue adding schools to it or make new array
if([loadedArray count] > 0)
{
    [loadedArray addObject:schools];

    > // it crashes here


    [schools release];

    return loadedArray;
} else
{ 

    //It will add the school to the array for the first time if there is nothing when it loaded.

    NSMutableArray *tempArray = [[[NSMutableArray alloc] init] autorelease];
    [tempArray addObject:schools];

    [schools release];

    return tempArray;
}

This function help add the school into favorite list. I cant add once, but it crashes when I add it again.

This is my code of loadData function

- (NSMutableArray *) loadData
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    NSMutableArray *list = [userDefault objectForKey:kSchoolList];
    
    return list;
    
}

The log does not say anything but this: Thread 1: Program received signal "SGABRT"

when I first run the programme and add, it is fine, I add school again then only it crashes, crashes at [loadedArray addobject:schools];

4
  • 1
    Could you post the crash log? Commented Mar 15, 2012 at 5:02
  • 1
    NSMutableArray *loadedArray = [self loadData]; make sure the array returned by [self loadData] is mutable too...other wise do this and try again NSMutableArray *loadedArray = [[self loadData]mutable copy]; Commented Mar 15, 2012 at 5:04
  • Try this NSMutableArray *loadedArray = [[NSMutableArray alloc] initWithArray:[self loadData]]; Commented Mar 15, 2012 at 5:19
  • hi, thanks for answering, mutableCopy is correct. It works fine now. Commented Mar 16, 2012 at 9:46

3 Answers 3

1

Your code looks clear. The only white spot is the way you create array in [self loadData] method.

You mentioned that you restore it from NSUserDefaults. Probably you just return it by [[NSUserDefaults standardUserDefaults] objectForKey:@"schools"].

Since objectForKey: method returns id type object you don't get a warning telling that objectForKey: returns NSArray not NSMutableArray.

Just make something like this:

return [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"schools"]];


I hope I guessed your code right ;)

Sign up to request clarification or add additional context in comments.

Comments

0

It would help if you put up the log, so we'll know what the crash actually is. I assume at some point you are storing values of tempArray into loaded. Try changing your code to this

 NSMutableDictionary *schools = [[NSMutableDictionary alloc] init];
    [schools setObject:name forKey:kFavoriteSchoolName];
    //load data is getting data from NSUserDefault which I save
    NSMutableArray *loadedArray = [[self loadData] retain];

//if loadedarray has object in there, then continue adding schools to it or make new array
if([loadedArray count] > 0)
{
    if([loadedArray isKindOfClass:[NSMutableArray class]]) {
    [loadedArray addObject:schools];
    }
    else {NSLog(@"Not a mutable array");}

    > // it crashes here


    [schools release];

    return [loadedArray autorelease];
} else
{ 
    [loadedArray release];
    //It will add the school to the array for the first time if there is nothing when it loaded.

    NSMutableArray *tempArray = [[[NSMutableArray alloc] init] autorelease];
    [tempArray addObject:schools];

    [schools release];

    return tempArray;
}

1 Comment

Thanks for fast reply, I just edited my question. Please take a look again and help me out.
0

I found out that you didn't initialize your array loadArray. You cannot add any value to uninitialized NSMutableArray.

You need to start with initialization.

NSMutableArray *loadArray  = [[NSMutableArray alloc]init];

Then populate data to your array

[loadArray  addObjectsFromArray:[self loadData]];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.