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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:08:42+00:00 2026-05-26T10:08:42+00:00

I realize this question may sound dumb, but just bear with me. I built

  • 0

I realize this question may sound dumb, but just bear with me. I built an app to help new developers wrap their head around memory retention on the iPhone (no ARC yet). It is plain and simple, 4 buttons, init, access, retain, and release. Pretty self explanatory. I am displaying what the retain count for my string object that is the target of our poking and prodding. (Please no lectures on use of [myVar retainCount], I already know)

This stuff will never make it into actual apps, just toying with it for fun and hopefully help someone learn how memory works. My retain and release all work great. My question is that why does my retain count drop back to 1 if I call myString = [[NSMutableString alloc] init]; again. I can boost my retain count to 40, but after calling alloc/init I go back to zero. I am not leaking anywhere, just curious what happens to myString if/when alloc/init is called on it again.

  • 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-26T10:08:42+00:00Added an answer on May 26, 2026 at 10:08 am

    Try this.

    NSString *myString = [[NSMutableString alloc] init];
    NSLog(@"%d", [myString retainCount]); // "1"
    for (int i = 1; i < 40; i++)
        [myString retain];
    NSLog(@"%d", [myString retainCount]); // "40"
    
    NSString *backup = myString;
    myString = [[NSMutableString alloc] init];
    NSLog(@"%d", [myString retainCount]); // "1"
    NSLog(@"%d", [backup retainCount]); // "40"
    

    You see, you have a different object with a new retain count. Your original object still exists and still has the same retain count. Assignment changes the object a variable refers to. A variable doesn’t have a retain count, an object does.

    myString = someOtherString;
    NSLog(@"%d", [myString retainCount]); // who knows?
    

    With retained property:

    self.iString = backup;
    NSLog(@"%d", [self.iString retainCount]); // "41" - 1 more because property retained
    NSString *newString = [[NSMutableString alloc] init];
    NSLog(@"%d", [newString retainCount]); // "1"
    self.iString = newString;
    // 1 for alloc 1 for retain (in real code you should release newString next)
    NSLog(@"%d", [self.iString retainCount]); // "2"
    NSLog(@"%d", [backup retainCount]); // "40" - self.iString released it
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize that this may sound like a silly question, but the last time
I realize this may be a rather heretical question, but I wonder whether I
I realize this may be a very simple question but I need to know
I realize this may be subjective, so will ask a concrete question, but first,
NOTE : In hindsight, I realize this may seem like a stupid question, but
Realize this may sound like a broad question - so let me clarify. I
I realize that this question may be a long shot, but I have to
This question may be obvious to some, but I am just beginning to scratch
I realize this question is pretty basic, but I'm really stuck. I have a
I realize this question has probably been asked numerous times, but I have not

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.