1

I created a singleton with contains an AVAudioPlayer object

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>

@interface drwAudioPlayer : NSObject <AVAudioPlayerDelegate>

@property AVAudioPlayer *aPlayer;

+(drwAudioPlayer *) sharedPlayer {

    static drwAudioPlayer *sharedPlayer = nil;
    if (!sharedPlayer) {
        sharedPlayer = [[super allocWithZone:nil] init];
    }
    return sharedPlayer;
}

-(void) setPlayerEpisode:(NSData *) episodeFile {
    NSError *error;
    aPlayer = nil;
    aPlayer = [[AVAudioPlayer alloc] initWithData:episodeFile error:&error];
}

In my main view I included the above class and created an object of type drwAudioPlayer. When I call [drwAudioPlayer.aPlayer setPlayerEpisode:foo], I see that the new file is getting loaded and the old one released.

But as soon as I start the playback of a file and then try to call [drwAudioPlayer.aPlayer setPlayerEpisode:foo2] the memory increases by the size of the new file being loaded.

What do I have to do to basically free up the memory before loading the new file to play.

I'm using ARC.

4

2 回答 2

0

似乎iOS6和模拟器都有问题。到目前为止,我只在模拟器中测试了我的应用程序是否泄漏内存,而不是在实际设备上。在 iPhone 上运行代码时,一切都像一个魅力,我的代码中没有内存泄漏。

我还在stackoverflow上发现了这些帖子,它们说的是同样的事情:

内存泄漏 AVAudioPlayer

iOS6 内存泄漏

于 2013-07-26T14:09:22.700 回答
-2

根据定义,单例是内存泄漏。不要使用它们。相反,请在代码中对音频播放器进行引用,当使用播放器的视图控制器消失时,播放器将被释放。在 iOS 下,内存不是垃圾收集器,你根本不能做这样的事情并期望内存被释放,ARC 不是垃圾收集器。

于 2013-07-13T01:41:35.863 回答