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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:33:44+00:00 2026-06-10T01:33:44+00:00

when not under ARC, for the following code, .h @property (nonatomic, retain) NSString *s;

  • 0

when not under ARC, for the following code,

.h

@property (nonatomic, retain) NSString *s;

.m

NSString *m = [NSString stringWithString:@"Hellow, World"];
s = [m retain];

// later on
s = nil; <-- will this release the ref count on the string and hence get the string released?
  • 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-10T01:33:46+00:00Added an answer on June 10, 2026 at 1:33 am

    When used like this (i. e. by accessing the instance variable directly), then no, it won’t. However, if you use the property’s accessor method ([self setS:nil]; or self.s = nil;), then yes, it will.

    Also note that releasing an object and freeing its memory are two completely different things. An object is deallocated only when it has no more strong references – i. e. you have the last reference to it and then you release it. If you release it but it has other references (by having been retained previously), then it won’t be deallocated yet, only its reference count will be decremented by one.

    Furthermore, if you have a retained property, such the one in your example, you must not do 1. access the underlying instance variable directly, 2. do stuff like

    NSString *m = [NSString stringWithString:@"Hellow, World"];
    s = [m retain];
    

    Why? Because the first line is simply unnecessary – really, why - [NSString stringWithString:]? You’re creating a constant string, then create an exact copy of it – it’s just superfluous. If Cocoa’s designers were noobs, this line would also waste memory – two exact copies of the same immutable string. Fortunately, whoever implemented NSString was prepared for this situation and made this method check its argument for being a constant and returning it without doing anything if it is – so you get back the same pointer, but with a few extra calls to objc_msgSend – that’s not something you want.

    The second line is also wrong – again, you don’t use the backing ivar as-is. Also, the property is declared retain for a reason – if you set an object to your property, that object will be retained by the setter method – no need to manually retain it.

    All in all, you’d better write

    self.s = @"Hello World";
    
    // ...
    
    self.s = nil;
    

    instead.

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

Sidebar

Related Questions

I am writing code intended to work both under ARC and under Garbage Collection.
With all this new ARC stuff (which does not fall under NDA…) coming out,
I have the following query that works under Hibernate but not under eclipse: select
The following (simplified version of our) code passes our JUnit tests under Hibernate, but
Instruments can visualize retain cycles under ARC in a graphically interesting way. I also
I have long-lasting TCP connection between two computers (second not under my control). Second
I'm wondering how other programmers are backing up changes that are not under source
So I'm trying to parse some XML, the creation of which is not under
I am having an issue with implicit conversions not working under certain circumstances (higher
I'm having trouble with Timeouts which do not work reliably under JRuby (Linux). More

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.