{"id":3848,"date":"2014-03-30T06:01:49","date_gmt":"2014-03-30T06:01:49","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/my-code-either-leaks-and-works-or-doesnt-leak-and-crashes-this-doesnt-seem-like-an-autorelease-problem-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:01:49","modified_gmt":"2014-03-30T06:01:49","slug":"my-code-either-leaks-and-works-or-doesnt-leak-and-crashes-this-doesnt-seem-like-an-autorelease-problem-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/my-code-either-leaks-and-works-or-doesnt-leak-and-crashes-this-doesnt-seem-like-an-autorelease-problem-collection-of-common-programming-errors\/","title":{"rendered":"My code either leaks and works, or doesn&#39;t leak and crashes. This doesn&#39;t seem like an autorelease problem-Collection of common programming errors"},"content":{"rendered":"<p>After I finished coding the difficult parts of my game, I found some memory management bugs.<\/p>\n<p>objects is a NSMutableArray holding a custom class.<\/p>\n<pre><code>- (void) spawnObjects\n{   \n    for (int index = 0; index &lt; INITIAL_OBJECTS; index++)\n    {\n        [objects addObject:[[[MatchObject alloc] initWithImageNameID:(index % 3)] autorelease]];\n        [[objects objectAtIndex:index] setPosition:[GameLayer randomPoint]];\n    }\n\n    ...\n}\n<\/code><\/pre>\n<p>I later use this function.<\/p>\n<pre><code>- (void) checkAllSprites\n{\n\n    NSMutableArray *spritesToDelete = [NSMutableArray array];\n    for (int index = 0; index &lt; [points count] - 1; index ++)\n    {\n        for (MatchObject *planetLike in objects)\n        {\n                CGPoint point1 = [[points objectAtIndex:index] CGPointValue];\n                CGPoint point2 = [[points objectAtIndex:index+1] CGPointValue];\n                if ([GameLayer lineIntersectsCircle:point1 :point2 :[planetLike position] :16.0f])\n                {\n\n                        ParticleSystem *planetDeath = [ParticlePlanetDeath node];\n                        planetDeath.texture = [[TextureMgr sharedTextureMgr] addImage:@\"fire.pvr\"];\n                        planetDeath.position = [planetLike position];\n                        [self addChild:planetDeath z:0 tag:2];\n\n                        [spritesToDelete addObject:planetLike];\n\n                        [self removeChild:planetLike cleanup:YES];\n\n                }\n\n        }\n    }\n    [objects removeObjectsInArray:spritesToDelete];\n    [spritesToDelete removeAllObjects];\n\n}\n<\/code><\/pre>\n<p>If I do not autorelease in the first function, the app works fine. If I do, then I try to access a dealloced object ([MatchObject position]).<\/p>\n<p>What&#8217;s wrong?!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After I finished coding the difficult parts of my game, I found some memory management bugs. objects is a NSMutableArray holding a custom class. &#8211; (void) spawnObjects { for (int index = 0; index &lt; INITIAL_OBJECTS; index++) { [objects addObject:[[[MatchObject alloc] initWithImageNameID:(index % 3)] autorelease]]; [[objects objectAtIndex:index] setPosition:[GameLayer randomPoint]]; } &#8230; } I later use [&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-3848","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3848","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=3848"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3848\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}