5

I am trying to play a click sound on every button click in my app For that i created a Utility class whose .h and .m is as follows

.h file

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

.m file

@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

and on button click on any class i am doing

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

Its working fine and i succeeded to play sound. Problem arises when i analysed the code. Compiler shows that there is memory leak in initWithDefaultClickSoundName method in .m of utility class as i am sending alloc method to self.audioplayer and not releasing it.

What is the best place of releasing this object?

abdus.me
  • 1,819
  • 22
  • 34

2 Answers2

2

The issue is when you alloc the object it's retainCount will be 1, you are assigning that object to a retain property object. Then it'll again retain the object hence the retainCount will be 2.

The setter code of a retain property is something like:

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

Change the :

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

like;

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

or like:

 AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:BS_path_blue]  error:NULL];
 self.audioplayer = player;
 [player release];
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • thanks for quick answer, i am trying to avoid autorelease, so if i implement second option you gave. In that case as i have retained **self.audioplayer** and retain count of audioplayer object is 1. Now i am assigning it to new object **player** whose retain count is also 1. Will it be the case that i am loosing reference of the object with retain count 1 after assigning new value to **self.audioplayer** ?? – abdus.me Dec 19 '12 at 13:58
  • after that I'm releasing the temporary object. so it won't create any orphans or cause leak – Midhun MP Dec 19 '12 at 14:01
  • Why avoid autorelease? A sound object is pretty much guaranteed to live longer than one pass through the run loop anyway and the cost of autorelease vs. playing a sound is negligible. Note that the retain count may or may not be 1 on allocation. Absolute retain counts are meaningless. – bbum Dec 19 '12 at 17:55
0
self.audioplayer =[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:BS_path_blue]  error:NULL];

Here, you create a new object, then assign it to a retained property. However, apart from the property, you have no reference to the object again, so it leaks. You've increased the retain count twice.

To fix, in order of preference:

  1. Convert to ARC ;)
  2. Create a local variable, assign it to the property, then release it.

    Object *object = [[Object alloc] init];
    self.property = object;
    [object release];
    
  3. Add a autorelease call to the object as you are adding it: self.property = [[[Object alloc] init] autorelease];

jrturton
  • 118,105
  • 32
  • 252
  • 268