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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:41:48+00:00 2026-05-26T16:41:48+00:00

I know that my question has already been discussed on StackOverflow but i found

  • 0

I know that my question has already been discussed on StackOverflow but i found the answer not complete for my needs. So the question is:

NSMutableArray *firstArray = [[NSMutableArray alloc] initWithObjects: obj1,obj2,nil];
NSMutableArray *secondArray = [[NSMutableArray alloc] init];
secondArray = [firstArray mutableCopy];

what is retain count for the secondArray now? 2 or 1? Should i release it twice or just once?
Does copy or mutableCopy increases retain count of the COPYING (secondArray in this event) object?

  • 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-26T16:41:49+00:00Added an answer on May 26, 2026 at 4:41 pm

    You should never care about the absolute retain count. Only that you’re “balanced”, that means for every alloc, new*, copy, mutableCopy and retain you need a corresponding release or autorelease (when not using ARC, that is).

    If you apply this rule to each line you can see that your second line has an alloc, but there’s no release. In fact, it’s absolutely useless to allocate an instance here since you’re not interested in it anyway. So it should simply read:

    NSMutableArray *firstArray = [[NSMutableArray alloc] initWithObjects: obj1,obj2,nil];
    NSMutableArray *secondArray = [firstArray mutableCopy];
    // There is no third line.
    

    But let’s discuss your original code and see what happened:

    NSMutableArray *firstArray = [[NSMutableArray alloc] initWithObjects: obj1,obj2,nil];
    NSMutableArray *secondArray = [[NSMutableArray alloc] init];
    // secondArray points to a new instance of type NSMutableArray
    secondArray = [firstArray mutableCopy];
    // You have copied another array (created a new NSMutableArray
    // instance) and have overwritten the pointer to the old array.
    // This means that the instance allocated in line 2 is still there
    // (was not released) but you don't have a pointer to it any more.
    // The array from line 2 has been leaked.
    

    In Objective-C, we often speak of ownership: there are very few methods that make you the “owner” of an object. These are:

    • alloc
    • new*, as in newFoo
    • copy and mutableCopy
    • retain

    If you call these, you get an object for which you are responsible. And that means you need to call a corresponding number of release and/or autorelease on these objects. For example, you’re fine if you do [[obj retain] retain]; and then [[obj autorelease] release];

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

Sidebar

Related Questions

I know that such question was has been many times already but I couldn't
I know a similar question has already been asked, but that retrieves all the
I know that this question has already been asked HERE but sadly none of
I know this question has probably been asked already but I would really like
I know that this question has been done to death at StackOverflow and that
I know this question has already been asked but I am in urgent need
I know that a question has already been asked about generating a unique ID
i know that this question has been posted a lot of times but the
I know this question has already been asked, but the solution doesn't work in
I know this question has been posed several times, but my goal is slightly

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.