5

アプリでボタンをクリックするたびにクリック音を鳴らそうとしています。そのために、.hと.mが次のようなユーティリティクラスを作成しました。

.hファイル

@interface SoundPlayUtil : NSObject<AVAudioPlayerDelegate,AVAudioSessionDelegate>
{
    AVAudioPlayer *audioplayer;   
}
@property (retain, nonatomic) AVAudioPlayer *audioplayer;
-(id)initWithDefaultClickSoundName;
-(void)playIfSoundisEnabled;
@end

.mファイル

@implementation SoundPlayUtil
@synthesize audioplayer;

-(id)initWithDefaultClickSoundName
{
self = [super init];
    if (self)
{
    NSString* BS_path_blue=[[NSBundle mainBundle]pathForResource:@"click"   ofType:@"mp3"];
    self.audioplayer =[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:BS_path_blue]  error:NULL];
   [self.audioplayer prepareToPlay];
}
return self;
}

-(void)playIfSoundisEnabled
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:soundStatus]==YES)
{
    [self.audioplayer play];
}
}

-(void)dealloc
{
[audioplayer release];
[super dealloc];
}
@end

ボタンをクリックして、私が行っているクラスをクリックします

 SoundPlayUtil *obj = [[SoundPlayUtil alloc] initWithDefaultClickSoundName];
 [obj playIfSoundisEnabled];
 [obj release];

それはうまく機能し、私は音を出すことに成功しました。コードを分析すると問題が発生します。コンパイラは、allocメソッドをself.audioplayerに送信していて解放していないため、ユーティリティクラスの.mのinitWithDefaultClickSoundNameメソッドにメモリリークがあることを示しています。

このオブジェクトを解放するのに最適な場所はどこですか?

4

2 に答える 2

2

問題は、retainCountが1になるオブジェクトを割り当てるときに、そのオブジェクトをretainプロパティオブジェクトに割り当てていることです。次に、オブジェクトを再び保持するため、retainCountは2になります。

保持プロパティのセッターコードは次のようなものです。

- (void)setAudioplayer: (id)newValue
{
    if (audioplayer != newValue)
    {
        [audioplayer release];
        audioplayer = newValue;
        [audioplayer retain];
    }
}

変更 :

self.audioplayer =[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:BS_path_blue]  error:NULL];

お気に入り;

self.audioplayer =[[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:BS_path_blue]  error:NULL] autorelease];

または好き:

 AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:BS_path_blue]  error:NULL];
 self.audioplayer = player;
 [player release];
于 2012-12-19T13:48:16.170 に答える
0
self.audioplayer =[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:BS_path_blue]  error:NULL];

ここでは、新しいオブジェクトを作成し、それを保持されたプロパティに割り当てます。ただし、プロパティを除いて、オブジェクトへの参照が再度ないため、リークが発生します。保持カウントを2回増やしました。

修正するには、優先順に:

  1. ARCに変換;)
  2. ローカル変数を作成し、それをプロパティに割り当ててから解放します。

    Object *object = [[Object alloc] init];
    self.property = object;
    [object release];
    
  3. オブジェクトを追加するときに、オブジェクトに自動解放呼び出しを追加します。self.property = [[[Object alloc] init] autorelease];

于 2012-12-19T13:49:06.383 に答える