-1

Possible Duplicate:
How do I use NSTimer
Decrement Issue

I'm really struggling to get rid of the following warning:

Incorrect decrement of the reference count of an object that is at this point not owned by the caller.

The code compiles fine and the app seems to work fine. Basically I am trying to create an object of a class to play a short audio clip when the button is pressed. I've created a class to play the file and the objects are passed the files name as a string.

Here is the code:

- (IBAction) playKick 
{        
    PlayAudio *thisPlayAudio= [[[PlayAudio alloc] init] playFile:(@"RockSnare")];
    [thisPlayAudio release];
}

I've read the other posts and any help would be much appreciated!

Community
  • 1
  • 1
Martin
  • 3
  • 3
  • Hey thanks for the response! Ive tried autorelease using this code- (IBAction) playKick { PlayAudio *thisPlayAudio = [[[[PlayAudio alloc] init] playFile:(@"file")] autorelease]; } but then I get an 'unused variable' warning and the 'analyzer' warning still doesn't go! – Martin Feb 23 '12 at 15:58
  • 1
    This code looks fine (if written a little unconventionally and possibly relying on implementation details). Are you sure the problem is with this method? What line does the warning point to? – Jasarien Feb 23 '12 at 16:01
  • 2
    Looks like he's asked the question twice. – Almo Feb 23 '12 at 16:06
  • 1
    Show the code for the playFile method. I'm guessing that it's probably returning an autoreleased object and it's not returning the instance of the PayAudio class that you are instantiating. If that's the case, you wouldn't be releasing what you think you are releasing. I think it's unusual to see code like this. I recomment separting the alloc/init from the playFile method call. Deal with releasing the instantiated object more explicitly. – Jim Feb 23 '12 at 17:53

4 Answers4

2

I'd have to see the class definition for PlayAudio, but it's doubtful the playFile method returns its PlayAudio instance. You probably want this:

PlayAudio *thisPlayAudio = [[PlayAudio alloc] init];
[thisPlayAudio playFile:(@"RockSnare")];
[thisPlayAudio release];
Art Gillespie
  • 8,747
  • 1
  • 37
  • 34
2

Maybe this would help:

- (IBAction) playKick {
    PlayAudio *thisPlayAudio= [[PlayAudio alloc] init];
    [thisPlayAudio playFile:(@"RockSnare")];
    [thisPlayAudio release];
}

This finishes creating the object and assigns it to thisPlayAudio, then plays the audio. What you have sets thisPlayAudio to the result of the playFile call.

Almo
  • 15,538
  • 13
  • 67
  • 95
  • Hey guys thank you so much for taking the time to respond! I eally appreciate it. I'm sorry I posted the question twice. I wasn't sure it posted properly initially. And thanks Almo! The above worked! I may be trying to run before I can walk! Best wishes all. Martin – Martin Feb 23 '12 at 20:29
  • The question seems to have been merged with the other, so you may want to select the answer that helped the most by clicking the checkmar. And now you know how the system works for the next time you need help. :) – Almo Feb 23 '12 at 21:02
1

What is the return type of the playFile: method? Are you sure it returns the same object you call it on?

Maybe your code should be:

PlayAudio *thisPlayAudio= [[PlayAudio alloc] init];
[thisPlaysAudio playFile:@"RockSnare"];
[thisPlaysAudio release];

or even

[[[[PlayAudio alloc] init] autorelease] playFile:(@"RockSnare")];
Amy Worrall
  • 16,250
  • 3
  • 42
  • 65
1

You are trying to immediately release object after creating. Probably, you also doing something wrong in playFile method. And your player won't play any file since you create and delete it in one scope. Try this:

PlayAudio *thisPlayAudio = nil;
- (IBAction) playKick {
    if (thisPlayAudio){
      [thisPlayAudio release]; thisPlayAudio = nil;
    }
    thisPlayAudio= [[PlayAudio alloc] init] autorelease];
    [thisPlayAudio playFile:(@"RockSnare")];
}
beryllium
  • 29,669
  • 15
  • 106
  • 125