{"id":5221,"date":"2014-03-30T19:51:37","date_gmt":"2014-03-30T19:51:37","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/ivar-and-property-release-in-dealloc-collection-of-common-programming-errors\/"},"modified":"2014-03-30T19:51:37","modified_gmt":"2014-03-30T19:51:37","slug":"ivar-and-property-release-in-dealloc-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/ivar-and-property-release-in-dealloc-collection-of-common-programming-errors\/","title":{"rendered":"ivar and property release in dealloc-Collection of common programming errors"},"content":{"rendered":"<p>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 <code>dealloc<\/code>?<\/p>\n<p>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?<\/p>\n<p>Example:<\/p>\n<pre><code>@interface MyClass: NSObject\n{\n     NSObject *ivar;   \/\/ This is sometimes omitted.\n}\n\n@property (nonatomic, retain) NSObject *ivar;\n\n@implementation MyClass\n@synthesize ivar;\n\n...\n\n-(void)dealloc\n{\n   [ivar release];\n   [super dealloc];\n}\n<\/code><\/pre>\n<p>How come the ivar declaration is sometimes omitted?<\/p>\n<p>The other case is when there is no property declaration:<\/p>\n<pre><code>@interface MyClass: NSObject\n{\n     NSObject *ivar;\n}\n\n@implementation MyClass\n\n-(void)thisMethodCanBeCalledOrNot\n{\n    ivar = [[NSObject alloc] init];\n\n    [ivar useIt];\n\n   \/\/ivar must be alive for further uses in different methods of this class. For this is not released in this method.\n}\n\n...\n\n-(void)dealloc\n{\n   [ivar release]; \/\/If thisMethodCanBeCalledOrNot is never called, could this cause a over release in ivar?\n   [super dealloc];\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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? [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5221","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5221"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5221\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}