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 4014688
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:31:38+00:00 2026-05-20T09:31:38+00:00

I have the following code running when the user clicks the Save button: –

  • 0

I have the following code running when the user clicks the “Save” button:

- (IBAction) onSaveChangesClick:(id)sender  {

NSMutableString *newGroups = [[NSMutableString alloc] init];


for (int i = 0; i < [self.isInGroupArr count]; i++) {
    if ([[self.isInGroupArr objectAtIndex:i] boolValue] == YES) {
        [newGroups appendString:[[AppDelegate arrayGroups] objectAtIndex:i]];
        [newGroups appendString:@","];
    }
}

//  remove last : ","
if ([newGroups length] > 0)
    newGroups = [NSMutableString stringWithString:[newGroups substringToIndex:[newGroups length] - 1]];


self.contact.groups = newGroups;
[newGroups release];
//[[self navigationController] popViewControllerAnimated:YES];
}

self.IsInGroups is BOOL array and arrayGroups is (NSString *) array that holds groups names.
I would like to add the newGroups string to the arrayGroups[i] only if (IsInGroups[i] == YES).

This piece of code generates EXC_BAD_ACCESS. WHY?

Thanks.

  • 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-20T09:31:38+00:00Added an answer on May 20, 2026 at 9:31 am

    Here you create autoreleased instance of NSMutable string.

    newGroups = [NSMutableString stringWithString:[newGroups substringToIndex:[newGroups length] - 1]];
    

    so you shouldn’t release it, and all will be fine.

    Here is improved code:

    - (IBAction) onSaveChangesClick:(id)sender  {
    
    NSMutableString *newGroups = [[[NSMutableString alloc] init] autorelease];
    
    
    for (int i = 0; i < [self.isInGroupArr count]; i++) {
        if ([[self.isInGroupArr objectAtIndex:i] boolValue] == YES) {
            [newGroups appendString:[[AppDelegate arrayGroups] objectAtIndex:i]];
            [newGroups appendString:@","];
        }
    }
    
    //  remove last : ","
    if ([newGroups length] > 0)
        newGroups = [NSMutableString stringWithString:[newGroups substringToIndex:[newGroups length] - 1]];
    
    
    self.contact.groups = newGroups;
    
    //[[self navigationController] popViewControllerAnimated:YES];
    

    }

    Explanation:

    1. Here you allocate memory and retain it.

      [[NSMutableString alloc] init]

    2. [NSMutableString stringWithString: returns autoreleased instance of NSMutable string, that we shouldn’t release(it does the same as [[[NSMutableString alloc] init] autorelease] + smth more). and you assign it to variable newGroups(so your old value that was stored in this variable lost)

    if ([newGroups length] > 0)
    newGroups = [NSMutableString stringWithString:[newGroups substringToIndex:[newGroups length] – 1]];

    1. newGroups here is autoreleased, you release it, and it destroys. But as it was autoreleased, autorelease pool tries to release it again and gets exception(because memory is allready free)

      [newGroups release];

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

Sidebar

Related Questions

I am running into ActiveRecord::ConnectionNotEstablished error. I have the following code ActiveRecord::Base.establish_connection(:adapter => 'sqlite3',
I have the following code: def show @game = $site.new_game_of_type(params[:id]) @game.start end def update_game_time
I have the following HTML code: <div id=summary_form> <textarea class=summary>Please fill in</textarea><br/> <textarea class=summary>Please
First, some visualization of the code. I have the following images that are dynamically
I have the following code: $('#DoButton').click(function (event) { event.preventDefault(); $(input:checked).each(function () { var id
I have a function called save(), this function gathers up all the inputs on
So I have a form that I want the user to use to update
I have an ASP.NET MVC 1.0 application running on Windows Server 2003 IIS 6.0.
I am facing the following problem. I have an image Gallery and I want
The first line of code, called on every one of my application's pages, is:

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.