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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:27:07+00:00 2026-05-13T22:27:07+00:00

Stupid question, but why do we need to use ‘retain’ when declaring a property?

  • 0

Stupid question, but why do we need to use ‘retain’ when declaring a property? Doesn’t it get retained anyway when it’s assigned something?

Looking at this example, it seems that an object is automatically retained when alloc’ed, so what’s the point?

#import "Fraction.h"
#import <stdio.h>

int main( int argc, const char *argv[] ) {
    Fraction *frac1 = [[Fraction alloc] init];
    Fraction *frac2 = [[Fraction alloc] init];

    // print current counts
    printf( "Fraction 1 retain count: %i\n", [frac1 retainCount] );
    printf( "Fraction 2 retain count: %i\n", [frac2 retainCount] );

    // increment them
    [frac1 retain]; // 2
    [frac1 retain]; // 3
    [frac2 retain]; // 2

    // print current counts
    printf( "Fraction 1 retain count: %i\n", [frac1 retainCount] );
    printf( "Fraction 2 retain count: %i\n", [frac2 retainCount] );

    // decrement
    [frac1 release]; // 2
    [frac2 release]; // 1

    // print current counts
    printf( "Fraction 1 retain count: %i\n", [frac1 retainCount] );
    printf( "Fraction 2 retain count: %i\n", [frac2 retainCount] );

    // release them until they dealloc themselves
    [frac1 release]; // 1
    [frac1 release]; // 0
    [frac2 release]; // 0

■output

Fraction 1 retain count: 1

Fraction 2 retain count: 1

Fraction 1 retain count: 3

Fraction 2 retain count: 2

Fraction 1 retain count: 2

Fraction 2 retain count: 1

Deallocing fraction

Deallocing fraction


This is driving me crazy!

  • 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-13T22:27:07+00:00Added an answer on May 13, 2026 at 10:27 pm

    The default behavior of a property is ASSIGN, not RETAIN. These are not the same behavior. If your property is for a primitive data type, such as an int, then ASSIGN would be correct to use. However, if you specify (retain) and the property points to an object pointer such as NSObject *object, the pointer is assigned the memory address of the object and it’s retain count is incremented by one. If your program consists of nothing more than a lone main function, then it’s purpose is hard to see. However, suppose your class had this method:

    -(void)setMyArrayWithString:(NSString *)s{
        myArray = [NSArray arrayWithObject:s];
        }
    

    Suppose myArray is defined as NSArray *myArray and has the proper @property (retain) statement.
    Everything works fine until the method returns. This is because the object returned from NSArray is an autoreleased object. If we don’t retain it, it will be released by the NSAutoReleasePool and we won’t be able to use it (and we’ll get nasty bugs and bad access violations). To fix this we can do one of two things:

    -(void)setMyArrayWithString:(NSString *)s{
        self.myArray = [NSArray arrayWithObject:s];
    // OR
        myArray = [[NSArray arrayWithObject:s] retain];    
    }
    

    The first solution uses self.myArray to utilize the @property definition. This code assigns and then retains the object so that we don’t lose it when the function returns. The second solution sets the NSArray *myArray pointer manually and then manually retains the NSArray object. Either way the NSAutoreleasePool will release the object at the end of the function, however it won’t be deallocated because we still have our retain from earlier keeping it alive.

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

Sidebar

Related Questions

Sorry for stupid question. but do I need to use standart model validation, such
I know you may think this question is stupid, but I need to use
This may be a stupid question to ask, but still I need to know
This might be a stupid question but I'll ask anyway, I was reading OOP
This may seem like a basic/stupid/obviously-answered question, but I wanted to check: why use
Sorry if this is a stupid question but new to this so need some
This may sound like a stupid question, but how can I start to use
This is likely a stupid question but I always find myself wondering which is
This might be a stupid question but I'm new at all this. I want
This may be a stupid question but im just starting to learn Rail thats

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.