Objective C [对象释放]

发布于 2024-09-29 05:51:14 字数 399 浏览 8 评论 0原文

我正在查看其他人的代码,但它似乎释放了对象 VIDEO,但随后继续使用它。

现在,根据我对面向对象编程语言的理解,一旦它被释放,它应该从内存中释放......

我看不出它如何有任何引用......但我假设这就是它没问题的原因。似乎是一件奇怪的事情,(当你还没有完成它时释放它,为什么不使用 autorelease 例如)。

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

// set output image size
video.outputWidth = 426;
video.outputHeight = 320;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

誰ツ都不明白 2024-10-06 05:51:14

它相当于:
self.video = [[[VideoFrameExtractor alloc] initWithVideo:[Utilities bundlePath:@"sophie.mov"]] autorelease];

(假设视频由 self 保留)

通过避免尽可能使用自动释放池,并且它有助于定位代码中有关引用计数的错误。 soo...假设属性是保留或复制,那么 self 应该恰好保存一个引用 - 这是完美的。

希望有帮助。

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.

第七度阳光i 2024-10-06 05:51:14
self.video = [[VideoFrameExtractor alloc] initWithVideo:[Utilities bundlePath:@"sophie.mov"]];

此行实际上调用 -setVideo: 方法,其中视频变量可能被保留(如果使用保留属性声明了相应的属性)。因此视频对象的保留计数变为 2,为了补偿额外的保留,我们释放它。仅当对象的保留计数变为 0 时,对象才会被释放,因此这样做是安全的。

我们还需要在代码中的某个位置(例如在 dealloc 方法中)释放视频,以确保视频对象在不需要时被销毁,以避免内存泄漏

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文