Re: Releasing by reference
Re: Releasing by reference
- Subject: Re: Releasing by reference
- From: Scott Morrison <email@hidden>
- Date: Wed, 1 Aug 2007 10:02:08 -0400
You need to read up on memory management.
http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/
index.html
Your +[QTMovie movieWithFile:error:] method is a factory method --
meaning that the object returned is autorealeased.
Very likely this will cause problems because it is essentially
passing back the onus of ownership of the object to the sender of the
message -- Leading very likely to crashes.
What you should be doing is returning an auto released object and
then retaining that autoreleased object in the sender so that
whatever object is ultimately using that movie knows about it.
ie:
Object in Class A:
-(void) someMethod{
if (_myMovieIVar) [_myMovieIVar release]; // prevent leak
_myMovieIVar = [QTMovie movieWithFile:@"/path" error:err];
// if this object is to make use of myMovie at any point in the
future it needs to retain it
[_myMovieIVar retain];
// now you can pass that pointer to somewhere else. it is up to the
receiver to retain it again if it needs it for any length of time.
[objectOfClassB openMovie: _myMovieIVar];
// alternatively, if ClassA doesn't need to make use of the movie at
anypoint in the future, pass the autoreleased result in to
objectOfClassB
[objectOfClassB openMovie: [QTMovie movieWithFile:@"/path" error:err]];
// notice no retain in doing so.
}
-(void)dealloc{
// if it has been retained, release ownership of it;
[_myMovieIVar release];
}
Class B
static QTMovie *mov = nil;
-(QTMovieView *)openMovie:(QTMovie *)aMovie{
if (mov && aMovie && mov!=aMovie) [mov release]; // prevent a leak;
mov = [aMovie retain]; // I am going to use this down the road.
// assuming ownership of the Movie in case objectOfClassA goes out
of scope;
}
-(void) dealloc{
if (mov) {
[mov release]; // I have
}
}
but here is a bigger question?
WHY STATIC --- potential problem is that if you have multiple
instances, and one gets dealloced, then mov will no longer be
available to other instances or it will crash unless you have checks
on mov every time you access it. Also this will NOT be thread safe.
// If it your intent to make this a class that is only instantiated
once -- then make it an ivar,
Memory management can be tricky but the key thing to remember that
each object should take care of the ownership of memory it is
using. AVOID at all costs anything where an object is created(or
retained) in once class/instance and to be released in another. --
Scott
On 1-Aug-07, at 9:16 AM, email@hidden wrote:
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;
}
}
On 8/1/07, Stephane Sudre <email@hidden> wrote:
On 1 août 07, at 14:02, email@hidden wrote:
Hello!
I'm initializing an object in class A, passing it to class B
where it
is
held in a static variable. I then release the object by
referencing the
variable. Does this also release the original object, or do I
have to
release it in class A?
The answer is probably no but it's difficult to be 100% affirmative
without additional information on your implementation.
_______________________________________________
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:
email@hidden
This email sent to email@hidden
_______________________________________________
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
________________________________
Scott Morrison <email@hidden>
Creator of Mail Act-On and Mail Tags plug-ins for OS X Mail.app.
<http://www.indev.ca/>
_______________________________________________
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