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 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

Ask A Question

Stats

  • Questions 354k
  • Answers 354k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to do this through VBScript and load in… May 14, 2026 at 7:52 am
  • Editorial Team
    Editorial Team added an answer EDIT: I tried to clarify I bit more ... 1.… May 14, 2026 at 7:52 am
  • Editorial Team
    Editorial Team added an answer Read each line, stick the contents of the line into… May 14, 2026 at 7:52 am

Related Questions

I have an odd problem. Our company collects data and we use a HORRIBLE
I've read a great deal of discussion recently (both on this site and elsewhere)
When compiling a 64bit application, why does strlen() return a 64-bit integer? Am i
For my software development programming class we were supposed to make a Feed Manager
For our software we use hardware dongles to protect the software. No protection is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.