i getting memory leak when click play button....
i testing "leak" tool under "run , performance tool"....on simulator
i getting leak when click play button first time.....
here code....
-(ibaction)play { [self setplayer]; [self playme]; } -(ibaction)stop { [self stopme]; [self releaseplayer]; } -(void)setplayer { nsurl *file = [[nsurl alloc] initfileurlwithpath: [[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:@"shut up.mp3"]]; nserror *err = nil; player = [[avaudioplayer alloc] initwithcontentsofurl:file error:&err]; [file release]; player.numberofloops = -1; [player preparetoplay]; player.volume=1.0; } -(void)playme { if (!isplaying) { [player play]; isplaying=yes; } } -(void)stopme { if (isplaying) { [player stop]; isplaying=no; } } -(void)releaseplayer { if(!isplaying) { [player release]; player=nil; } isplaying=no; }
i think, below statement source of memory leak,
player = [[avaudioplayer alloc] initwithcontentsofurl:file error:&err];
here posts has discussed same issue.
avaudioplayer memory leak - media player framework
here blog post
edited:
as per blog tutorial code must below.
-(void)setplayer { nsurl *file = [[nsurl alloc] initfileurlwithpath: [[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:@"shut up.mp3"]]; nserror *err = nil; nsdata *data = [nsdata datawithcontentsoffile:file]; avaudioplayer *player = [avaudioplayer alloc]; if([player initwithdata:audiodata error:null]) { player.numberofloops = -1; [player preparetoplay]; player.volume=1.0; [player autorelease]; } else { [player release]; player = nil; } [file release]; }
the leak-free version stores pointer returned alloc, rather pointer returned initwithdata:error:. way, whatever happens, player can still released.
Comments
Post a Comment