Apple 代码中缺少发布版本?

发布于 2024-12-28 19:16:11 字数 230 浏览 0 评论 0原文

在此 Apple 代码中: http://developer.apple.com/library /ios/#qa/qa1702/_index.html

我可以看到在init方法开始时分配的会话没有释放。

为什么 ?这是有原因的吗?

In this Apple code: http://developer.apple.com/library/ios/#qa/qa1702/_index.html

I can see that the session allocated at the start of the init method is not released.

Why ? Is there a reason to this ?

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

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

发布评论

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

评论(1

樱&纷飞 2025-01-04 19:16:11

我想到了几个可能的原因:

  • Apple 打算将此示例转换为 ARC,但忘记或错过了稍后在同一代码段中的 autorelease 调用。
  • init 方法末尾获取 session 的 ivar 被声明为 @property (assign),因此他们故意希望保留它目前。如果我没记错的话,这是一种糟糕的做法 - 他们应该有 autoreleased 会话并声明 @property (retain)
  • 他们稍后手动释放会话。严格来说,这并不是不好的做法,但肯定会令人困惑且难以阅读,并且可能会导致以后维护中的错误(当有人失去对 retain-release 平衡的跟踪时)。
  • 这确实是一个错误。好捕获!

无论如何,你是对的,这与良好的内存管理实践有些不一致。然而,很难确定它是否有明确的原因没有发布。

A couple possible reasons come to mind:

  • Apple meant to convert this sample to ARC, and forgot about or missed the autorelease call later in the same snippet.
  • The ivar that gets session at the end of the init method is declared @property (assign), and so they deliberately wanted to keep it retained for now. If I remember right, this is poor practice - they should have autoreleased session and declared the @property (retain).
  • They hand-release the session later. Not strictly poor practice, but certainly confusing and hard to read, and may lead to a bug in maintenance later (when someone loses track of the retain-release balance).
  • This is genuinely a bug. Good catch!

In any event, you're right in that it's somewhat inconsistent with good memory management practices. It's hard to tell for sure, however, whether there's a definitive reason it's not released.

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