Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone EXC_BAD_ACCESS with NSMutableArray

Ok so I have a UIViewTable and a UISearchBar with two scope buttons. The idea being that when I press a scope button the datasource for UIViewTable gets changed but I am getting EXC_BAD_ACCESS error.

I have the following code in my UIViewController SearchViewController.m:

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange: (NSInteger) selected scope
{
    MyAppDelegate *delegate = (MyAppDelegate *) [[UIApplicationsharedApplication] delegate];
    if (self.listData != nil) {
        [self.listData release];
    }
    if (selectedScope == 0) {
        self.listData = [delegate.data getListOne];
    }
    else {
        self.listData = [delegate.data getListTwo];
    }
}

- (void) viewDidLoad {
    MyAppDelegate *delegate = (MyAppDelegate*) [[UIApplication sharedApplication] delegate];
    self.listData = [delegate.data getListOne];

    //some other unrelated code
}

in my SearchViewController.h I have:

@property (nonatomic,retain) NSMutableArray *listData;

in my Data.m I have:

-(NSMutableArray *) getListOne {
     NSMutableArray *list = [[NSMutableArray alloc] initWithObjects:@"test1",
                                                                    @"test2",
                                                                    nil];
     [list autorelease];
     return list;
}

-(NSMutableArray *) getListTwo {
     NSMutableArray *list = [[NSMutableArray alloc] initWithObjects:@"test3",
                                                                    @"test4",
                                                                    nil];
     [list autorelease];
     return list;
}

It crashes on:

self.listData = [delegate.data getListTwo];

I checked that its when I am setting the property that it crashes. My understanding is that when I create the new NSMutableArray in Data.m I assign it autorelease as I should.

When the view loads I assign it to my listData and since I am accessing the property which has retain then the reference count is incremented (so its now 2 pending auto release).

When I press the button to change the data source I check too see if listData exists (which it always will), release it so that the old NSMutableArray counter will be 0 (assuming autorelease has occured).

Then I get a new NSMutableArray and set it to this property... is my understanding correct? I have spent far too long on this simple problem :(

oh also I did create another NSMutableArray that was not connected to the tableView and still get the same problem, also if I don't release it in my if statement the problem does not exist but I will then have a memory leak?? I could always just keep the array and remove/add objects but I want to know why this is not working :) cheers

like image 406
Allan Avatar asked Jul 07 '09 12:07

Allan


1 Answers

This is your problem:

if (self.listData !=nil)
{
    [self.listData release];
}

You don't need to do this check -- by virtue of the fact that you declared the listData property with the retain property, the synthesized setter automatically takes care of releaseing the old value. The synthesized setter would look something like this:

- (void) setListData:(NSMutableArray *)listData
{
    [listData retain];
    [self->listData release];
    self->listData = listData;
}

Note a few things here: the old value is released, and the new value is retained. Further, the retain happens before the release, in case of self-assignment: if you assign the same value, you don't want it to be prematurely deallocated. Also note that if either the new or old value is nil, nothing bad happens, since Objective-C explicitly permits you to send messages to nil, having no effect.

So, this means that whenever you set the property, you don't need to worry about releasing the old value -- the setter does that for you. Because you are doing an extra release, the object is getting deallocated before you're actually done using it, so as soon as you use it after it's been deallocated, you get the EXC_BAD_ACCESS.

like image 65
Adam Rosenfield Avatar answered Oct 26 '22 23:10

Adam Rosenfield