{"id":3867,"date":"2014-03-30T06:08:11","date_gmt":"2014-03-30T06:08:11","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/iphone-avaudioplayer-nsurl-memory-management-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:08:11","modified_gmt":"2014-03-30T06:08:11","slug":"iphone-avaudioplayer-nsurl-memory-management-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/iphone-avaudioplayer-nsurl-memory-management-collection-of-common-programming-errors\/","title":{"rendered":"iPhone: AVAudioPlayer\/NSURL memory management-Collection of common programming errors"},"content":{"rendered":"<p>I posted a much longer question a few minutes ago, and as it usually goes as soon as I posted it I realized what was going on, so I deleted it since most of the post was irrelevant. Then I went back to Google.<\/p>\n<p>Turns out I&#8217;m having almost the same exact problem as described in this post, unanswered from June. http:\/\/www.iphonedevsdk.com\/forum\/iphone-sdk-development\/20975-avaudioplayer-nsurl-memory-management.html<\/p>\n<p>In summary: I&#8217;m using AVAudioPlayer and releasing it with the audioPlayerDidFinishPlaying:successfully: delegate method. After I initialize the player, its associated NSURL object needs to be freed, otherwise it leaks. But when I release it after initializing the player, it crashes since it has already been freed. The weird thing is that it doesn&#8217;t always crash the first time, most of the time it crashes after the second sound has been played. Sometimes (rarely) it will allow a handful or players to be alloc\/released (I reuse the pointers after freeing) before crashing. Any help?<\/p>\n<p>Code snippet: (soundKeyUp is an AVAudioPlayer* class variable, hence no declaration here)<\/p>\n<pre><code>NSString *soundKeyUpPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@\"soundKeyUp%d\", key.tag % 5] ofType:@\"wav\"];\n    NSURL *soundKeyUpURL = [[NSURL alloc] initFileURLWithPath:soundKeyUpPath];\n    soundKeyUp = [[AVAudioPlayer alloc] initWithContentsOfURL:soundKeyUpURL error:nil];\n    if(soundKeyUp) {\n        [soundKeyUp setDelegate:self];\n        [soundKeyUp play];\n    }\n    else {\n        [soundKeyUp release];\n        soundKeyUp = nil;\n    }\n\n    [soundKeyUpPath release];\n    [soundKeyUpURL release];\n<\/code><\/pre>\n<p>Response to Steve Riggins: The trick of it is that it doesn&#8217;t crash at the same time every time, as mentioned. It almost always properly releases the first time (or at least, it doesn&#8217;t crash or leak) but usually after the second time I allocate\/release the player and URL, it crashes on releasing the URL. Sometimes, it goes 3, 4, 5+ times before crashing, but it always does.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I posted a much longer question a few minutes ago, and as it usually goes as soon as I posted it I realized what was going on, so I deleted it since most of the post was irrelevant. Then I went back to Google. Turns out I&#8217;m having almost the same exact problem as described [&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-3867","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3867","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=3867"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3867\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}