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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:40:06+00:00 2026-06-17T21:40:06+00:00

NSArray *arr = [[NSArray arrayWithObjects:@ag, @sdfg, @dgh, nil] retain]; NSArray *newArr = [[arr mutableCopy]

  • 0
NSArray *arr = [[NSArray arrayWithObjects:@"ag", @"sdfg", @"dgh", nil] retain];
NSArray *newArr = [[arr mutableCopy] retain];
[arr release];
[newArr release];
newArr = [NSArray arrayWithObject:@"sdfdhs"];

What will retain count after each line? Please explain this to me. Thanking You…

  • 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-17T21:40:08+00:00Added an answer on June 17, 2026 at 9:40 pm

    Retain counts are pretty much useless, see http://whentouseretaincounts.com for details of why.

    However, I added calls to retainCount to your code and ran the following:

    NSArray *arr = [[NSArray arrayWithObjects:@"ag", @"sdfg", @"dgh", nil] retain];
    NSLog(@"%ld", [arr retainCount]);
    
    NSArray *newArr = [[arr mutableCopy] retain];
    NSLog(@"%ld", [newArr retainCount]);
    
    [arr release];
    NSLog(@"%ld", [arr retainCount]);
    
    [newArr release];
    NSLog(@"%ld", [newArr retainCount]);
    
    newArr = [NSArray arrayWithObject:@"sdfdhs"];
    NSLog(@"%ld", [newArr retainCount]);
    

    and got the following results:

    2013-01-24 15:45:56.840 Untitled 2[96774:707] 2 
    2013-01-24 15:45:56.842 Untitled 2[96774:707] 2 
    2013-01-24 15:45:56.842 Untitled 2[96774:707] 1 
    2013-01-24 15:45:56.843 Untitled 2[96774:707] 1 
    2013-01-24 15:45:56.843 Untitled 2[96774:707] 1
    

    The first result is 2 not 1 because the return value from arrayWithObjects has been autoreleased but hasn’t actually been released yet because the autorelease pool has not been flushed yet (this usually happens in the event loop).

    The second result is 2 because the mutableCopy returned a retained object and we are retaining it again.

    The third result is 1 because we release arr which had a retain count of 2. Still haven’t flushed the autorelease pool.

    The fourth result is 1 because we released newArr which had a retain count of 2.

    The final result is 1 because we leaked the contents of newArr and assigned a new autoreleased array to the variable. The retain count of 1 is the not yet autoreleased count.

    However, retain counts should not be trusted. Learn the memory management rules (whether you are using ARC or not).

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

Sidebar

Related Questions

NSArray *arr = [NSArray arrayWithObjects:@m, @f, @m, @f, @f, nil]; How can I put
NSArray arrayWithObjects needs nil at the end, NSString stringWithFormat and NSLog() doesn't. Why? [NSArray
NSArray* arr = [NSArray array]; int a = MIN([arr count], 1); // 0 correct
i have an array like this: NSArray *arr = [[NSArray alloc]initWithObjects:@1,@2,@3,@4,@5,@6,nil]; how i can
I have an NSArray with over 4.000 CGPoints [NSArray arrayWithObjects: [NSValue valueWithCGPoint:CGPointMake(213, 30)], ...
will autorelease release my non-object c array? I am wondering, because perhaps only objects
- (NSArray *) makeKeyValueArray: (NSArray *) arr { NSMutableArray *result = [[NSMutableArray alloc] init];
Can someone see where the error is here... NSArray *arr = [str componentsSeparatedByString:@,]; NSString
Does ARC ever inject retain and release calls that you generally wouldn't see in
I have an NSArray arr . It has a bunch of NSNumber objects. I'm

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.