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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:26:51+00:00 2026-05-31T14:26:51+00:00

If I declare an ivar without property declaration, and this ivar could be used

  • 0

If I declare an ivar without property declaration, and this ivar could be used or not during the object life-cycle, do I have to release it in dealloc?

I have sometimes seen that properties are declared as ivar and property, and sometimes only have property declaration. What is the difference? Which is the better way?

Example:

@interface MyClass: NSObject
{
     NSObject *ivar;   // This is sometimes omitted.
}

@property (nonatomic, retain) NSObject *ivar;

@implementation MyClass
@synthesize ivar;

...

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

How come the ivar declaration is sometimes omitted?

The other case is when there is no property declaration:

@interface MyClass: NSObject
{
     NSObject *ivar;
}

@implementation MyClass

-(void)thisMethodCanBeCalledOrNot
{
    ivar = [[NSObject alloc] init];

    [ivar useIt];

   //ivar must be alive for further uses in different methods of this class. For this is not released in this method.
}

...

-(void)dealloc
{
   [ivar release]; //If thisMethodCanBeCalledOrNot is never called, could this cause a over release in ivar?
   [super dealloc];
}
  • 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-31T14:26:53+00:00Added an answer on May 31, 2026 at 2:26 pm

    Yes you have to release it. When you call [ivar release], nothing happens if the ivar is nil, but if it contains a pointer to an object, you would cause a memory leak by not releasing it.

    It’s preferable to not use self.ivar = nil in the dealloc method, as the setter method could contain logic that is unnecessary during deallocation, or could even cause tricky bugs.

    Regarding @property declarations without an explicit ivar declaration, the “modern runtime” synthesizes the ivar automatically.

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

Sidebar

Related Questions

If I declare a property strong, like so: @property (strong, nonatomic) UIView *iVar; When
A quick question. if I have a property and an ivar declared with the
I have declared this ivar in my ViewController.h #import <UIKit/UIKit.h> @interface FirstViewController : UIViewController
Let's say I have a class. I would like to declare a property in
I have an ivar in my Obj-C class that's a C array (I'm not
I am developing one application in that I have declared iVar as NSIndexPath object,but
Update - Many people are insisting I need to declare an iVar for the
I have created a normal app - initially without cocos2d. Now I need cocos2d
In my Cocoa application, in the header file, I declare a NSString ivar: NSString
I usually see this question asked the other way, such as Must every ivar

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.