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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:30:36+00:00 2026-05-25T12:30:36+00:00

I have an NSMutableArray containing many objects. What happens if a change is made

  • 0

I have an NSMutableArray containing many objects.

What happens if a change is made to the array, while I am making a copy of the array using [NSMutableArray arrayWithArray:someArray];

Eg: If an object an object is removed from the array while the copy is being made?

I’m not sure how to test this scenario.

EDIT: The objects are not released (as they are retained elsewhere). I just use this array as a lookup table.

  • 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-25T12:30:37+00:00Added an answer on May 25, 2026 at 12:30 pm

    as you know, the container/collection is not guaranteed to be thread safe. what could happen if you change the array while copying or reading? a lot of things. the obvious cases are that it may be reallocating at the time, it may pass or return an invalid reference to you (e.g. the most recently removed), or it may access objects which have been released (from another thread). in addition to things that will make your app crash or cause other UB, it may not return correct or consistent values. it is a misinterpretation of the data. neither are good.

    you don’t test the scenario – threading issues are difficult to reproduce and you really can never cover all the cases. since the object itself does not guaranteee thread safety – your implementation has to restrict accesses/mutations/interactions to one thread at a time. when dealing with object which are used in multithreaded contexts: anytime you access or query information from an object’s mutable state, you should guard the object (e.g. with a lock). therefore, you simply lock it while using it. lock/copy/unlock/use copy is also common. for an NSMutableArray, examples of mutable state would be all its objects and its count. its operations and mutations also use the object’s mutable state, so they are restricted.

    if you only use this object from one thread, then obviously don’t need to lock it. this is also a reason why passing by copy and holding immutable variants are both good ideas in most cases. you don’t need a lock for each object, a guard for the object which holds it is often a good way to design a class for thread safety.

    Update

    …What happens? Does the copy contain all 5 objects (since they are retained elsewhere anyway? Do they contain 4? (even containing 4 is sufficient for me) Is an exception thrown ?

    if you have not properly guarded the collection, it’s as good as undefined behavior and you’re lucky if it crashes.

    you need to take the appropriate precautions to avoid undefined behavior. your program is operating in that domain when it’s not properly guarded.

    to elaborate: retaining the objects externally only reduces the probability of undefined behave\ior, but it certainly does not eliminate it. more examples of consequences include exceptions, segfaults, reading or writing memory which is used as another active allocation (which may show up as very mysterious issues which may also be virtually impossible to reproduce).

    i encourage you to guard properly or take another approach. UB is EVIL 🙂

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

Sidebar

Related Questions

I'm currently dynamically creating UIButton objects in my view. I have an NSMutableArray containing
I have an NSMutableArray I am trying to populate using a for-loop and making
hello I have a NSMutableArray iconlocarr . I also have another array containing dictionary
Newbie question. I have a NSMutableArray that holds multiple objects (objects that stores Bezier
I have a class that subclasses NSMutableArray. I init it using: MyClass class =
I have an ivar, keys which is an NSMutableArray containing 50 strings. When my
I have an NSMutableArray which has about 18 objects. They are in a specific
I have an NSMutableArray something like this array =[[a1,b1,c1],[a2,b2,c2],[a3,b3,c3].......] and I access this array
For example say I have: NSMutableArray *array = [[NSMutableArray alloc] init]; NSMutableArray *array2 =
Have just started using Google Chrome , and noticed in parts of our site,

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.