Apple 更改了命名约定的内存管理规则
如果您使用名称以“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有些方法在其文本中包含“复制”,但显然不是复制方法。例如,
+[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 withcopy
ormutableCopy
, 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.