ARC的Objective-C命名约定和可能的警告 [英] Objective-C naming conventions with ARC and possible caveats

查看:92
本文介绍了ARC的Objective-C命名约定和可能的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用纯ARC编码的经验.作为一种编译器功能,它尊重Objctive-C方法家族在需要时进行正确的保留/释放调用.

I have experience with pure ARC coding. As a compiler feature it honors Objctive-C method family putting right retain/release calls whenever neeeded.

allocmutableCopycopynew开头的所有方法都将创建一个新对象.他们增加了保留人数.结果,当我不再需要它时,ARC将释放任何指针(并因此释放与之关联的对象).

All methods that start with alloc, mutableCopy, copy and new create a new object. They increase the retain count. As a consequence, ARC will release any pointer (and hence the object associated with it) when I no longer need it.

我认为当我编写不遵循命名约定的方法时可能会出现问题.例如,如果我编写类似newCustomer的方法,则在第一个版本中返回一个自动释放的对象,而在第二个版本中则不返回一个自动释放的对象,

I think that problems could arise when I write methods that do not follow naming conventions. For example, if I write a method like newCustomer that in a first version returns an autoreleased object while in a second version does not, what could happen?

特别是,我的问题如下(它们属于相同的推理):

In particular, my questions are the following (they belong to the same reasoning):

  • 如果调用和被调用代码都使用ARC编译会怎样?
  • (a)如果使用ARC编译调用代码而使用非ARC编译被调用代码会怎样?
  • (b)如果使用非ARC编译调用代码而使用ARC编译被调用代码会发生什么情况?
  • What happens if the calling and called code are both compiled with ARC?
  • (a) What happens if the calling code is compiled with ARC while the called is compiled with non-ARC?
  • (b) What happens if the calling code is compiled with non-ARC while the called is compiled with ARC?

很高兴看到一个答案,它显示了ARC在后台(objc_releaseobjc_retainAutoreleasedReturnValue等)如何工作.

It would be appreciated an answer that shows how ARC works under the hood (objc_release, objc_retainAutoreleasedReturnValue, etc.).

谢谢.

推荐答案

名为newCustomer的方法将属于new

A method named newCustomer would fall within the new method family and is thus implicitly marked as returning an retained object. When both calling and called code is compiled with ARC, then ARC balances the extra retain with a release in the caller:

从此类函数或方法返回时,ARC保留该值 在评估return语句时,离开所有 本地范围.

When returning from such a function or method, ARC retains the value at the point of evaluation of the return statement, before leaving all local scopes.

当从此类函数或方法收到返回结果时,ARC 在包含完整表达式的末尾释放该值 内,并遵循通常对局部值的优化.

When receiving a return result from such a function or method, ARC releases the value at the end of the full-expression it is contained within, subject to the usual optimizations for local values.

来源

如果newCustomer是通过手动引用计数实现的,并且违反了命名约定(即不返回保留的对象),则调用者可以根据情况而释放过多或释放不足.

If newCustomer is implemented with manual reference counting and violates the naming convention (i.e., does not return a retained object), then the caller can either over release or under release, depending on the circumstances.

如果调用者使用ARC,则从newCustomer返回的对象将被过度释放-可能导致程序崩溃.这是因为调用代码将参与上述过程的后半部分,而之前没有执行相应的保留.

If the caller uses ARC, then the object returned from newCustomer will be overreleased - likely causing the program to crash. This is because the calling code will participate in the second half of the above process, without having had a corresponding retain performed prior to that.

如果调用代码不是使用ARC编译的,而是被调用的代码(因此正确实现了返回保留对象),则行为取决于程序员遵循的命名约定.如果他们释放返回的值,则将正确管理对象的引用计数.但是,如果程序员认为他们的new...方法确实违反了命名约定,并且未能在调用代码中手动插入发行版,则返回的对象将泄漏.

If the calling code is not compiled with ARC, but the called code is (thus correctly implementing returning a retained object), then the behavior depends on the programmer following the naming conventions. If they release the returned value, then the object's reference count will be correctly managed. However, if the programmer believes that their new... method does violate the naming convention, and fails to manually insert a release in the calling code, then the object that was returned will leak.

总而言之,正如Martin R.在评论中指出的那样,关键的决定是在任何环境中(包括手动引用计数)是否都遵循命名约定.

All in all, as Martin R. points out in the comments, the critical determination is whether the naming conventions are followed in any environment including manual reference counting.

这篇关于ARC的Objective-C命名约定和可能的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆