4

When I use AudioToolBox for playing music, memory leaks heavily.

AVAudioPlayer *newMusicPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];

I use this code to play music. In iOS5 and iOS4, it works properly. But in iOS6, if data's size is 5M, all of the 5M leaked. And I can't see leak info in Instruments.

Is there anybody have the same problem? Any suggestion will be grateful.

All my audio code here(Using ARC):

@implementation ViewController
{
    AVAudioPlayer *_player;
}

- (void)play
{
    if (_player)
    {
        [_player stop];
        _player = nil;
    }

    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"test.app/1.mp3"];
    NSData *musicData = [[NSData alloc] initWithContentsOfFile:path];
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:musicData error:nil];
    player.volume = 1;
    if (player)
    {
        _player = player;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(100, 100, 100, 100);
    [button setTitle:@"play" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

@end
OpenThread
  • 2,096
  • 3
  • 28
  • 49
  • check release notes exactly there has made some changes! at ios6 –  Sep 23 '12 at 14:28
  • Do you observe the memory leak in the iOS simulator or a device? Or both? – Jere Käpyaho Sep 23 '12 at 14:49
  • In simulator. In fact, I can't get objc symbols on device in 4.5's Instruments, only c symbols show – OpenThread Sep 23 '12 at 15:05
  • @matheszabi i didn't find it in https://developer.apple.com/library/ios/#releasenotes/General/iOS_DevLib_Release_Notes/_index.html#//apple_ref/doc/uid/TP40011648 is there any reference to it? Thanks – OpenThread Sep 23 '12 at 15:10
  • @OpenThread for the ios6 here is the link: https://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/_index.html MediaPlayer and Audio section, if those classes has nothing with your class, than I am wrong –  Sep 23 '12 at 15:18
  • @matheszabi too bad it didn't metion about AVAudioPlayer, thanks anyway – OpenThread Sep 23 '12 at 15:31
  • @OpenThread have you the AVAudioPlayer source code, do you know what he use? :) –  Sep 23 '12 at 15:39
  • @matheszabi Of course not, it's a black box. I edited my question and added my code :) – OpenThread Sep 23 '12 at 15:47
  • 1
    @OpenThread Several askers here on SO (and me) have had the same issue with the simulator only. Try to profile your app on a device, and if it doesn't leak there, then don't worry about it. – Jere Käpyaho Sep 23 '12 at 16:38
  • @JereKäpyaho my device running iOS6 leaks too T_T – OpenThread Sep 24 '12 at 02:43
  • @JereKäpyaho any reference to codes doen't leak in device running iOS6 is grateful – OpenThread Sep 24 '12 at 03:08

2 Answers2

5

I'm playing audio from a file using AVAudioPlayer, and found that the iOS simulator consistently leaks memory, but the device doesn't. This was verified using Instruments. This is ARC code.

My player is declared like this:

@property (nonatomic, retain) AVAudioPlayer *numberPlayer;

and synthesized like this:

@synthesize numberPlayer = _numberPlayer;

Since I need to play several different sounds, and AVAudioPlayer cannot be reset to play a different audio file after its creation, I'm creating a new player every time, like this:

NSString *audioFilePathName = [NSString stringWithFormat:@"Voices/%@/%03i.m4a", self.voiceName, self.theNumber];
NSURL *url = [NSURL fileURLWithPath:BUNDLE_FULL_PATH(audioFilePathName)];
self.numberPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
self.numberPlayer.numberOfLoops = 0;
[self.numberPlayer setCurrentTime:0.0];
[self.numberPlayer setDelegate:self];
[self.numberPlayer prepareToPlay];
[self.numberPlayer play];

In my delegate I set the player to nil when it has finished playing:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    if (player == self.numberPlayer) {
        _numberPlayer = nil;
    }
}

In the simulator, both AudioSessionDevice and UISoundNewDevice leak memory. However, the biggest leaker is actually NSURL. None of these leaks occur on the device. This behavior has not changed in iOS 6, but I do have my project deployment set to 5.0, should that make any difference.

See also https://stackoverflow.com/questions/3433487/avaudioplayer-leak-in-simulator and memory leak in AudioToolbox library AVAudioPlayer.

Community
  • 1
  • 1
Jere Käpyaho
  • 1,305
  • 1
  • 10
  • 29
  • 3
    initWithContentsOfURL: saves megabytes of memory, and only initWithData: leaks megabytes. I'm going to replace initWithData with initWithContentOfURL in my project. Thanks :) – OpenThread Sep 24 '12 at 06:25
  • Thank you, you are right, the simulator shows an AudioToolbox leak but when I run on a device the leak is not present. – codingCartooningCPA Nov 08 '22 at 15:16
1

Maybe you did better than me and I haven't the XCode in my front, but :

object created, reference created:

AVAudioPlayer *newMusicPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];

it is assignment and not allocation, no retain:

_musicPlayer = newMusicPlayer;

out of function playMusicWithMusicData it would deallocate the newMusicPlayer and it would crash my code in other function when I use the _musicPlayer. The analyzer tool nagged so much as I made a retain, maybe a release too.

if you are using ios4, than isn't ARC this project.

in if branch:

    [_musicPlayer stop];
    _musicPlayer = nil;

I would check the _musicPlayer to not be nil, but that will not solve your problems.

No clue where it could be, if not in ios and probably there it will be:

Did solve if you change the delegate instead of self.

 _musicPlayer.delegate = self;

to any other class?

  • I created a test project and pasted its code in question, it has the same problem with my music project, leaks in iOS6 and normal in iOS4/5. I didn't even set any delegate to AVAudioPlayer, it doesn't make difference T___T – OpenThread Sep 24 '12 at 02:49