0

I have a UIViewController. At the top of the UIViewController, I have declared

NSMutableArray *contacts;

In my viewDidLoad method, I call [self getContacts] which basically initializes my contacts array. It begins by initializing the array, and then it adds some objects:

if(contacts == nil)
   contacts = [[NSMutableArray alloc] init];

[contacts removeAllObjects];
[contacts addObjectsFromArray:[some objects]];

So, now my contacts is initialized. In my viewDidLoad method, I even use contacts, and it works great. Later on, in a method, I need to retrieve the elements of contacts, however I am getting an EXC_BAD_ACCESS. Why is this? Why doesn't my contacts array keep the objects that I initialized it with in the beginning, and how do I fix this?

EDIT: The error comes when I select a NavigationBarItem which then triggers a method buttonWasPressed. In that method, I simply have the following:

-(void)buttonWasPressed:(id)sender {
    NSLog(@"button was pressed");
    if(contacts == nil)
       NSLog(@"contacts is nil!");

    NSLog(@"contacts = %@",contacts);
}

And I see "button was pressed" printed, but then there is an EXEC_BAD_ACCESS.

1
  • 1
    Please post the code of that method where you get bad access. Also when you add objects to array, it retains those objects. Commented Jun 10, 2011 at 14:16

1 Answer 1

2

That code all looks good, nothing wrong there. I would guess you are over-releasing elsewhere. Turn on Zombies - add NSZombieEnabled to YES in the executable arguments and it will break on the line so you can see what object is being over-released.

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

4 Comments

how to I add NSZombieEnabled? where do I go in XCode to do this?
Expand Executables item in the Groups and Files list, double-click your executable, select the Arguments tab and add a value into the bottom window... Name - NSZombieEnabled value YES
where is "Groups and Files list"?
On the left, I see my project name, I see "Targets", "Executables", "Find Results", "Bookmarsk", "SCM", "Project Symbols", "Implementation Files", and "Interface Builder Files"

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.