views:

36

answers:

2

I'm looking at someone else's code, but it appears to RELEASE object VIDEO but then continue to use it.

Now from my understanding of Object Oriented Programming languages, once it's released, it should be dealloc'd from memory...

I can't see how it has any references...but I'm assuming that's the reason it's OK. Seems like a strange thing to do, (Release it when you haven't finished with it, why not use autorelease for example).

self.video = [[VideoFrameExtractor alloc] initWithVideo:[Utilities bundlePath:@"sophie.mov"]];
[video release];

// set output image size
video.outputWidth = 426;
video.outputHeight = 320;
+3  A: 
self.video = [[VideoFrameExtractor alloc] initWithVideo:[Utilities bundlePath:@"sophie.mov"]];

this line actually calls -setVideo: method where video variable is likely being retained (if corresponding property is declared with retain attribute). So retain count of video object becomes 2 and to compensate extra retain we release it. Object gets dealloced only when its retain count becomes 0, so it is safe to do so.

We also need to release video somewhere in the code (e.g. in dealloc method) to be sure video object gets destroyed when it is not needed to avoid memory leak

Vladimir
+4  A: 

it's equivalent to: self.video = [[[VideoFrameExtractor alloc] initWithVideo:[Utilities bundlePath:@"sophie.mov"]] autorelease];

(assuming the video is retained by self)

there's a small performance boost by avoiding autorelease pools where possible, as well as it helps localize errors in your code regarding ref counting. soo... assuming the property is retain or copy, then self should hold exactly one reference - which is perfect.

hope that helps.

Justin
Ah thanks, that sets the story straight.
David van Dugteren