Apple 更改了命名约定的内存管理规则

发布于 2024-10-30 01:39:35 字数 638 浏览 1 评论 0原文

Cocoa 内存管理规则

如果您使用名称以“alloc”或“new”开头或包含“copy”的方法(例如,alloc、newObject 或 mutableCopy)创建该对象,或者向该对象发送一个保留消息。

2010 年 12 月之后没有读过,但此后已更改为

您可以使用名称以“alloc”、“new”、“copy”或“mutableCopy”开头的方法(例如,alloc、newObject 或 mutableCopy)“创建”对象。

请注意,现在需要以“copy”作为前缀。这导致了来自 Clang Static Analyzer 的一些与内存相关的警告:(。在搜索互联网后,我还没有得出为什么要改变这一点的结论,因为这是 iOS 内存管理的基础之一。

有人知道吗?知道为什么吗?

As stated in Cocoa Memory Management Rules from before

You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message.

haven't read it after December 2010, but since has changed since then to

You “create” an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy).

Notice that now, it is required to have "copy" as a prefix. This resulted to a few memory related warnings from Clang Static Analyzer :(. After searching the interwebs, I haven't got to a conclusion as to why was this changed since this is one of the base foundations of Memory Management for iOS.

Does anybody know why? Thanks!

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

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

发布评论

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

评论(1

谁的新欢旧爱 2024-11-06 01:39:35

有些方法在其文本中包含“复制”,但显然不是复制方法。例如,+[NSData dataWithBytesNoCopy:length:]。当然,可以使用静态分析器的注释来通知非标准行为,但总的来说,我怀疑几乎没有人(除了你自己)曾经编写过不以 copy 或 mutableCopy,因此他们决定简化事情。

坦率地说,我很高兴他们这样做了,因为我遇到了相反的问题,其中一个方法包含“复制”一词,但并不打算返回拥有的引用。

There were some methods that contained "Copy" in their text, but were clearly not copying methods. For example, +[NSData dataWithBytesNoCopy:length:]. It was, of course, possible to use annotations for the static analyzer to inform about the nonstandard behavior, but in general I suspect that almost nobody (yourself excepted) ever wrote a copy method that didn't start with copy or mutableCopy, so they decided to just simplify things.

I'm glad they did, frankly, as I've run into the opposite problem, where a method contained the word "Copy" but was not intended to return an owning reference.

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