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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:30:21+00:00 2026-05-16T17:30:21+00:00

i have some questions about objective-c’s memory management, let’s say: NSString * test= [[NSString

  • 0

i have some questions about objective-c’s memory management,

let’s say:

NSString * test= [[NSString alloc] init ]
test=@"msg";
[object setStr1: test ]; // declared as: @property(copy, readwrite)
[object setStr2: test ]; // declared as: @property(retain, readwrite)
[object setStr3: test ]; // declared as: @property(assign, readwrite)

test=@"some other string"

I think str1 will have a copy of tests content: str1 will point to one adress of the memory (heap) that contais msg, this address is not the same pointed by test. right?

about str2:
1. what does it store?, i guess the same address that points test, but it will increase the reference counter of test to 2.
2. when i change the test’s content, what does str2 have? I guess it still points to msg

about str3: it’s incorrect, right?, what does assign do?

thanks.

bonus question:

NSString * test= [[NSString alloc] init ]
test=@"msg";
test=@"something";

should i release test before changing its content?

  • 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-16T17:30:21+00:00Added an answer on May 16, 2026 at 5:30 pm

    The most important thing to take away here: The assignment operator = never mutates (i.e. changes) an object. Mutating an object can only be accomplished by sending it messages (e.g., sending appendString: to an NSMutableString). The assignment operator simply causes a pointer to point to a different object than it did before.

    Thus, it is incorrect to say:

    (1) NSString * test = [[NSString alloc] init];
    (2) test = @"msg";
    

    Line (1) creates an NSString object, and assigns test to point to it. Line (2) does the same thing: it creates a new, unrelated NSString object, and assigns test to point to it. Now the original NSString created by line (1) has nothing pointing to it, and is leaked.

    Also, you never need to alloc a string literal; the compiler does this implicitely when you use the @"..." syntax. In general, you will very rarely have to use [NSString alloc] at all (only when you want to use the various init* methods, such as initWithFormat:, etc.)

    1. str1 will point to a distinct copy of the test string. (Errata: According to Eiko, the receiver will simply treat this as a ‘retain’ if it is immutable. This makes no practical difference if you are behaving correctly.)
    2. str2 will point to the same location as test, and the retain count of the object there will be incremented.
    3. str3 will point to the same location as test, but the retain count will not be incremented.

    Generally speaking, strings are immutable, so you cannot change their content. You may have to watch out for instances of NSMutableString, however, which is a subclass of NSString. This is why many people recommend copying strings instead of retaining them, so that, should the string be mutated by another part of the program, your object’s copy will be unaffected.

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

Sidebar

Related Questions

I have some questions about synthesized properties in Objective-C. The full list follows, but
I'm trying to understand Objective-C properties and I have some lingering questions about their
I have some questions about basic CSS that I was unable to understand or
I have some questions about customers about NF mode for DB2. Google had very
I have some questions about multi-threaded programming and multi-core usage. In particular I'm wondering
I have some questions about logging, more specifically about setting it up and making
I have some questions about Perl's map function. Specifically: How does %hash = map
I have some questions about partitioning and strategy, how and when to use it.
There have been some questions about whether or not JavaScript is an object-oriented language.
SO I already know about memory management in objective C, and I never had

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.