Re: Releasing by reference
Re: Releasing by reference
- Subject: Re: Releasing by reference
- From: "Paolo Manna" <email@hidden>
- Date: Wed, 1 Aug 2007 14:50:28 +0100
> The implementation is something like this:
> Object in class A: [QTMovie movieWithFile:@"/path/to/mov" error:err]
>
> Sent as parameter aMovie to class B:
> static QTMovie *mov = nil;
>
> - (QTMovieView *)openMovie: (QTMovie *) aMovie {
> mov = aMovie;
> }
>
> - dealloc {
> if ( mov != nil ) {
> [mov release];
> mov = nil;
> }
> }
This is wrong: movieWithFile: returns an autoreleased object (as all
"convenience constructors" do), so you should either retain it in
openMovie: (if you want to keep it around) or avoid to release it in
dealloc (if it's used shortly after openMovie:). Of course, if you
decide to retain it, you should also take care in openMovie: about the
release of any previous instance around, i.e.
- (QTMovieView *)openMovie: (QTMovie *) aMovie {
if (aMovie != mov) {
[mov release];
mov = [aMovie retain];
}
}
See the usual:
http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/index.html
> If openMovie method is called more than one time from Class A, then in that case there
> would be a Memory leak.
No: movieWithFile:error: is a "convenience constructor", following the
rules the object will be autoreleased
> 2. Pass autorelease object to Class B.
It is already, see above.
Paolo
_______________________________________________
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