Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 262381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:30:39+00:00 2026-05-11T22:30:39+00:00

Ok so I have a UIViewTable and a UISearchBar with two scope buttons. The

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:30:39+00:00Added an answer on May 11, 2026 at 10:30 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 138k
  • Answers 138k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can also shortcut that by just outputting the management… May 12, 2026 at 7:37 am
  • Editorial Team
    Editorial Team added an answer Use either m2eclipse or IAM (formerly Q4E). Both provide (amongst… May 12, 2026 at 7:37 am
  • Editorial Team
    Editorial Team added an answer Check out Open Inviter: http://openinviter.com. It has Yahoo, Hotmail, and… May 12, 2026 at 7:37 am

Related Questions

Ok so I have a number of methods that look like this:- which sorts
OK so I have a C++ class that is exposed to Lua using SWIG.
ok so i have a folder that was created after this answer was used
Ok, so I have a very simple form with next to no logic in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.