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

  • Home
  • SEARCH
  • 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 8157547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:19:20+00:00 2026-06-06T17:19:20+00:00

I was learning about property, I read various documents but still not clear about

  • 0

I was learning about property, I read various documents but still not clear about how property works and came across scenario which confused me.I created one sample app in which I created one property as follows:

@property(nonatomic,retain)NSString *strValue;

and synthesize it:

@synthesize strValue;

1)First Scenario:

In viewDidLoad I wrote:

strValue = [[NSString stringWithFormat:@"value"] retain];

In dealloc I wrote:

NSLog(@"str value : %@",self.strValue);
[self.strValue release];

It worked fine without any leak.My question here is: What happened to memory which is retained when I created retained property strValue(@property(nonatomic,retain)NSString *strValue;)?

2)Second Scenario:

In viewDidLoad I wrote:

self.strValue = [[NSString stringWithFormat:@"value"] retain];

In dealloc I wrote:

NSLog(@"str value : %@",self.strValue);
[self.strValue release];

It showed memory leak at self.strValue = [[NSString stringWithFormat:@”value”] retain] line.Question here is:Why it is showing memory leak here?Will this line is not equivalent to following lines of code:

[strValue release];
[strValue retain];

3)Third Scenario:
In viewDidLoad I wrote:

self.strValue = [NSString stringWithFormat:@"value"];

In dealloc I wrote:

NSLog(@"str value : %@",self.strValue);
[self.strValue release];

It worked fine without any memory leak or dangling reference, how?
Can anyone explain how property actually works?How memory is assigned and released when we use property?

  • 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-06T17:19:21+00:00Added an answer on June 6, 2026 at 5:19 pm

    First question,

    Since you are setting the instance to the variable itself and not to the property then you will have to allocate (or retain) the given instance, if you gave this variable an autoreleased object, later on the property will become zombie

    Second question

    No its not similar since the property already retains the instance, another retain will increase the retain count by an additional one, so you will have an additional retain count that will not be released ever.

    Third question

    As i said earlier the property will retain the instance so there will be no problem passing an auto released instance to it

    here is a sample retain property setter

    - (void) setProperty:(BookItem *)prop
    {
        if(_property != prop)
        {       
            [_property release];//release old
            _property = prop;
            [prop retain]; //retain new
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am still mostly unfamiliar with Inversion of Control (although I am learning about
I'm learning about DDD, and have come across the statement that value-objects should be
So I'm new to Objc-C and I'm just now learning about using @property and
I was trying something out as I was learning about @property and @synthesize .
Learning about notifyAll made me question something about notify: in a typical situation we
After learning about the Action delegate in C# I've been looking for ways I
I am learning about raw sockets. I heard that ping utility is using raw
I'm learning about C++11 concurrency, where my only prior experience with concurrency primitives was
I am learning about Algebraic DTs in haskell. What I would like to do
I'm learning about the MVC architecture these days and I'm confused MVC with the

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.