{"id":4053,"date":"2014-03-30T07:21:43","date_gmt":"2014-03-30T07:21:43","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/why-isnt-this-code-leaking-collection-of-common-programming-errors\/"},"modified":"2014-03-30T07:21:43","modified_gmt":"2014-03-30T07:21:43","slug":"why-isnt-this-code-leaking-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/why-isnt-this-code-leaking-collection-of-common-programming-errors\/","title":{"rendered":"Why isn&#39;t this code leaking?-Collection of common programming errors"},"content":{"rendered":"<pre><code>for(NSString *collectionName in jsonObjects)\n{\n  NSDictionary *collection = [[NSDictionary alloc] init]; \/\/ Allocation \n  collection = [jsonObjects valueForKey:collectionName]; \/\/ overwriting above, LEAKS\n  NSArray *items = [[NSArray alloc] initWithArray:[collection valueForKey:@\"items\"]]; \/\/ Allocation\n  NSNumber *lastAccess = [[NSNumber alloc] init]; \/\/ Allocation\n  lastAccess = [collection valueForKey:@\"lastAccess\"]; \/\/ overwriting above, LEAKs\n  [items release];\n}\n<\/code><\/pre>\n<p>calls that don&#8217;t contain &#8216;alloc&#8217; or &#8216;new&#8217; return references to object that are &#8216;autoreleased&#8217; which means they are not owned by you, releasing them will cause a crash. You can use them but only in the scope of the function that you are in, if you need them for later use you will need to call <code>retain<\/code> on them. So when calling accessor functions you only need to declare the variables that you need and not create new objects.<\/p>\n<p><code>alloc<\/code> creates a new object, in the case of <code>collection<\/code> you are creating an empty <code>NSDictionary<\/code>, but you promptly overwrite the reference to that new object with the autoreleased reference you get from <code>valueForKey:<\/code> this will create a leak. As the originally created object won&#8217;t get released.<\/p>\n<p>probably should look like this<\/p>\n<pre><code>for(NSString *collectionName in jsonObjects)\n{\n  NSDictionary *collection = [jsonObjects valueForKey:collectionName];\n  NSArray *items = [collection valueForKey:@\"items\"];\n  NSNumber *lastAccess = [collection valueForKey:@\"lastAccess\"];\n}\n<\/code><\/pre>\n<p>I realize that with the capability of having questions like this answered by a willing crowd reading the manuals has become just a chore but if you want to progress you should have a look, most of the apple documentation is exceptionally well written as these things go. The memory management guide will resolve a lot of questions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>for(NSString *collectionName in jsonObjects) { NSDictionary *collection = [[NSDictionary alloc] init]; \/\/ Allocation collection = [jsonObjects valueForKey:collectionName]; \/\/ overwriting above, LEAKS NSArray *items = [[NSArray alloc] initWithArray:[collection valueForKey:@&#8221;items&#8221;]]; \/\/ Allocation NSNumber *lastAccess = [[NSNumber alloc] init]; \/\/ Allocation lastAccess = [collection valueForKey:@&#8221;lastAccess&#8221;]; \/\/ overwriting above, LEAKs [items release]; } calls that don&#8217;t contain &#8216;alloc&#8217; or [&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-4053","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4053","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=4053"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4053\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}