• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Retain/Release and Properties clarification
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Retain/Release and Properties clarification


  • Subject: Retain/Release and Properties clarification
  • From: John Tsombakos <email@hidden>
  • Date: Mon, 03 Oct 2011 10:14:47 -0400

Hello,
I have a question about release/retain and properties. Now I get the
whole "if you allocate it, you have to release
it", but with properties I need some clarification. I'm using an
AVAudioPlayer to play a sound, and I'm initializing
it in the viewDidLoad by calling a routine to get the sound. Some sample code:

In my view controller .h file :
@interface AudioPlayerViewController : UIViewController {
AVAudioPlayer *audioPlayer;
}
@property (retain) AVAudioPlayer *audioPlayer;

In the .m file:
@synthesize audioPlayer;
in viewDidLoad:
audioPlayer = [self getSoundFile:"soundfile.wav"];
...

in getSoundFile routine:
AVAudioPlayer *snd;
...
snd = [[[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]
autorelease];
...
return snd;

Now (I did copy the code from a sample, that had the autorelease in
it) if I try to play the
sound, it crashes. If I remove the autorelease it's fine. I just want
to understand what's going on.
I know using the (retain) in the property declaration will use retains
when setting the property. Assuming
the autorelease is not there, in my getSoundFile routine, the
alloc/init would return a retained object
(correct?) Then in my viewDidLoad routine when I get the sound, does
the assignment do another retain?
Should I keep the autorelease and assign it with:
   audioPlayer = [[self getSoundFile:"soundfile.wav"] retain];

(side note... should that be self.audioPlayer = ... ? I keep getting
confused with accessors vs. ivars too ;) )

I hope that made sense :)  Thanks for any input / hints.
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Retain/Release and Properties clarification
      • From: Andreas Mayer <email@hidden>
    • Re: Retain/Release and Properties clarification
      • From: Steve Sisak <email@hidden>
  • Prev by Date: Re: [SOLVED] Re: Mystery of the missing symbol
  • Next by Date: Re: Retain/Release and Properties clarification
  • Previous by thread: Re: Debugging stack traces, reprised
  • Next by thread: Re: Retain/Release and Properties clarification
  • Index(es):
    • Date
    • Thread