I have a method called getProjects. In my method I parse data from json. I save name and id of the project in my instance variable projectsArray which is an NSMutableArray.
The problem is that I have a button (IBAction) called writeFile and if I try to log the array on click, my app crashes. The error message is ESC_BAD_ACCESS. But why? I am using ARC.
`Method getProjects
- (void)getProjects {
int count = 0;
self.projectsArray = [NSMutableArray arrayWithCapacity:10];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@projects.json", urlPath]]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSArray *projects = [parser objectWithString:json_string error:nil];
for (NSDictionary *project in projects) {
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:2];
[dict setObject:[project objectForKey:@"name"] forKey:@"name"];
[dict setObject:[project objectForKey:@"id"] forKey:@"id"];
[self.projectsArray insertObject:dict atIndex:count];
[self.selectProject addItemWithTitle:[project objectForKey:@"name"]];
count++;
}
}
And I call the method in applicationDidFinishLaunching
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[self getProjects];
NSLog(@"%@", self.projectsArray);
}
The log in my applicationDidFinishLaunching returns my array.
Well, it seems you have already found the cause of the issue:
The reason is that by declaring the property as
assign, when you set it, its value is not retained.So:
you create the array like this:
arraywithCapacitywill give you an autoreleased object;the object is not retain when assigning it to the property;
at some point in time, before the NSLog is executed, the object is deallocated.
crash.