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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:02:20+00:00 2026-06-15T18:02:20+00:00

@interface Foo : NSObject @property (nonatomic, retain) Bar * bar; @end @implementation Foo @synthesize

  • 0
@interface Foo : NSObject

@property (nonatomic, retain) Bar * bar;

@end

@implementation Foo

@synthesize bar = _bar;

- init {
    self = [super init];
    if (self) {

        _bar =  [[Bar alloc] init];

        // Or 

        _bar =  [[[Bar alloc] init] autorelease];


    }
    return self;
}

- (void)dealloc {
    [_bar release];
    [super dealloc];
}

@end

When I run the analyzer, both

        _bar =  [[Bar alloc] init];

and

        _bar =  [[[Bar alloc] init] autorelease];

are fine.

Which one I should use?

  • 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-15T18:02:21+00:00Added an answer on June 15, 2026 at 6:02 pm

    You should use the first. It creates a retained object, whereas the second “autoreleases” that retain.

    The important consideration is that you’re assigning it to instance variable _bar. If you were, instead, assigning it to property self.bar, then the retain directive in the property declaration would cause the object to be retained, so assigning an autoreleased value would be appropriate. But since you’re assigning to the “bare” instance variable instead, you need to handle the retain yourself, so you need the first form.

    PS: I’m a bit surprised that the analyzer doesn’t complain about the second version.

    PPS: It should be noted that the choice here is highly context dependent. But you included enough context (the property definition) to make the choice. Without seeing the property definition (or other info, in the case of a non-property) it would be hard to say.

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

Sidebar

Related Questions

@interface foo: NSObject @property (nonatomic, retain) NSMutableArray *aMutableArray; @end @implementation @synthesize aMutableArray -(void)somefunction {
@interface Foo : NSObject { extern int gGlobalVar; int i; } -(void)setgGlobalVar:(int)val; @end @implementation
@interface A : NSObject - (void) tmp; - (void) foo; @end @implementation A -
Assume that I have this piece of code: @interface Foo : NSObject { Bar
I have an inteface Foo with an implementation Bar. The interface Foo has a
I have created a class named Foo : @interface Foo:NSObject{ int myInt; } @property
@interface Foo : NSObject { } - (Bar)bar; At runtime, given [Foo class], how
@protocol protoA <NSObject> @end @interface objA : NSObject<protoA> { @private } @end @implementation objA
lets say I have aclass @interface Foo :NSobject { NSstring *a; NSDate *b; }
I have a NSArray of Foo objects. @interface Foo : NSObject { } -

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.