Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
"ESC_BAD_ACCES" - I don't know about such an error message. (Hint: ortography) –  user529758 Aug 4 '12 at 14:14
    
What class is writeFile in and, if it's not the app delegate, how does the object running that method get its reference to the projects array? –  Phillip Mills Aug 4 '12 at 14:17
    
The property is declared as assign and writeFile is running the appDelegate. I'm sorry, i mean "EXC_BAD_ACCESS". –  emha Aug 4 '12 at 14:24
1  
I changed assign to retain and now it works. But why? –  emha Aug 4 '12 at 14:29

1 Answer 1

up vote 0 down vote accepted

Well, it seems you have already found the cause of the issue:

I changed assign to retain and now it works. But why?

The reason is that by declaring the property as assign, when you set it, its value is not retained.

So:

  1. you create the array like this:

      self.projectsArray      = [NSMutableArray arrayWithCapacity:10];
    
  2. arraywithCapacity will give you an autoreleased object;

  3. the object is not retain when assigning it to the property;

  4. at some point in time, before the NSLog is executed, the object is deallocated.

  5. crash.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.