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

  • SEARCH
  • Home
  • 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 9013059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:09:38+00:00 2026-06-16T03:09:38+00:00

When replacing a value at some index with a new value within an NSMutableArray,

  • 0

When replacing a value at some index with a new value within an NSMutableArray, the old value is held in memory. The hack to fix is to initialize a new NSMutableArray before every loop.

Steps to Reproduce:

- (id) init{
    self.overlays = [[NSMutableArray alloc] initWithCapacity: [self.anotherArray count]];
}

- (void) someOtherMethod{
    for(int i = 0 ; i < self.anotherArray ; i++){
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [view setBackgroundColor:[UIColor colorWithRed:0 
                                                green:0 
                                                 blue:0 
                                                alpha:1]];
        [view setAlpha: .2];
        [self.overlays insertObject:view atIndex: i]
    }
}

- (void) main{
    for(int i = 0 ; i < 4 ; i++){
        [myObject someOtherMethod];
    }
}

insertObject:atIndex effectively causes a memory leak, because it does not release the old value in the array at that index.

I filed a bug report and Apple responded:

insertObject:atIndex: is behaving as defined. It is doing an insertion, not a replacement. If you want replacement, you should instead use -replaceObjectAtIndex:withObject:

How could the insertObject:atIndex: ever be of any benefit, as you always lose the reference to the old object at that index.

Is this simply to avoid fixing the issue as it meets old documentation definitions?

  • 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-06-16T03:09:39+00:00Added an answer on June 16, 2026 at 3:09 am

    The two methods do different things. Imagine the following array:

    NSMutableArray *anArray = [@[ @1, @2, @3 ] mutableCopy];
    

    If you insert an element at position 1, like so:

    [anArray insertObject:@4 atIndex:1];
    

    The array becomes equal to @[ @1, @4, @2, @3 ]. The new element is inserted without removing another element.

    If, instead, you replace the element at position 1, like so:

    [anArray replaceObjectAtIndex:1 withObject:@4];
    

    You get @[ @1, @4, @3 ]. The previous object at that location is removed.

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

Sidebar

Related Questions

While replacing a pattern in a string, I specifically need the integer/long value of
I am replacing some deprecated Javadoc annotation in code I am unfamiliar with. Most
I'm replacing some text with cufon on my page and I'm using jquery stylish
I'm having difficulty replacing the text of a link with new text. I have
So I was tasked at replacing some dummy code that our project requires for
I'm profiling some old java code and it appears that my caching of values
$(editor[i])[0].outerHTML has a value of: <p style=color: red; data-mce-style=color: red;>some string</p> I want data-mce-style=color:
I have some code as follows: <f:loadBundle basename=messages.application var=prop /> <ui:param name=currentUserAttr value=#{prop['currentUser']} />
I have 100,000 strings each with a fixed ordered index value like this: Index
I'm using a sed command for replacing some words in text files. This command

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.