本文介绍了使用第三个库代码时ARC如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关ARC的文章.以下是该部分:

I was reading article related to ARC.Following is the section:

  1. 分配,
  2. 复制
  3. mutableCopy
  4. 和新的
  1. alloc,
  2. copy,
  3. mutableCopy
  4. and new

被认为是转让的所有权 返回给调用者的对象.这意味着在您的应用程序中 您创建方法后,ARC会自动推断是否返回 方法名称中的自动释放对象或+1保留对象.但是,有一个小警告.假设您有一个 以"copy"开头的方法,如

are considered to be transferring ownership of the returned object to the caller. This means, in your application, when you create a method, ARC automatically infers whether to return a autoreleased object or a +1 retained object from your method name. However, there is a >small caveat. Let’s assume that you have a method that starts with "copy", as in

-(NSString *)copyRightString;

-(NSString*) copyRightString;

ARC假定它将把返回的字符串的所有权转移给调用者,并插入一个发行版. 自动地.如果被调用的方法和 调用方法是使用ARC编译的.

ARC assumes that it would transfer the ownership of the returned string to the caller and inserts a release automatically. Everything works well, if both the called method and the calling method are compiled using ARC.

但是,如果您的 "copyRightString"方法位于第三方库中 使用ARC编译后,您将过度释放返回的字符串.这是 因为,在调用代码上,ARC编译器将发行版插入到 平衡通过复制"方法增加的保留计数.

But if your "copyRightString" method is in a third party library that isn’t compiled with ARC, you will over-release the returned string. This is because, on the calling code, ARC compiler inserts a release to balance out the retain count bumped up by the "copy" method.

相反,如果第三方库是使用ARC和您的 方法不是,您将发生内存泄漏.

Conversely, if the third party library is compiled with ARC and your method isn’t, you will have a memory leak.

现在我有两个问题:1.当"copyRightString"方法在未使用ARC编译的第三方库中时,为什么对象被过度释放?由于方法名称以copy开头,因此该方法将不会释放对象,因为这是调用方法的责任释放由ARC负责的对象,因为方法的名称以copy开头.

Now I have two question:1.Why object is over-released when "copyRightString" method is in a third party library that isn’t compiled with ARC?Since method name starts with copy, so the method will not release the object because it is the responsibility of calling method to release the object which ARC will take care since the name of method begins with copy.

2.如果使用ARC和我们的第三方编译第三方库,为什么会发生内存泄漏方法不是吗?因为方法名称以copy开头,所以ARC不会释放它,并且有责任调用方法来释放它.在我们的代码中,我们将其释放,因为方法名称以copy开头.

2.Why there will be memory leak if the third party library is compiled with ARC and ourmethod isn’t?Since method name begins with copy so ARC will not release it and it is responsibility of calling method to release it.and in our code we will release it since then method name begins with copy.

我希望我很清楚!

推荐答案

在此示例中,选择方法名称-copyRightString来显示某个问题.方法签名表明该方法返回一个包含版权信息的字符串,因此程序员希望它返回一个自动释放的值.但是方法名称偶然以copy开头,因此ARC希望它返回保留值.方法的实现可能看起来像这样:

In this example, the method name -copyRightString was chosen to show a certain problem. The method signature suggests that the method returns a string containing copyright information, so programmers would expect that it returns an autoreleased value. But accidentally the method name starts with copy, so ARC expects it to return a retained value. A method implementation could look like this:

- (NSString *)copyRightString
{
   return [NSString stringWithFormat:@"Copyright %d %@",
                                     self.copyRightYear,
                                     self.companyName];
}

如果在没有ARC的第三方库中编译此方法,则该方法将返回自动释放的值.如果现在从ARC代码中使用它,则ARC会看到名称以copy开头,因此它希望此方法返回保留的(非自动释放的)值.因此,它将过于频繁地释放返回值.那就是过度释放的部分.

If you compile this method in a third party library without ARC, the method will return an autoreleased value. If you now use it from ARC code, ARC sees that the name starts with copy, so it expects this method to return a retained (not autoreleased) value. Because of that, it will release the return value one time too often. That's the over-release part.

另一方面,如果使用ARC编译此方法,则ARC会看到名称以copy开头,因此可以确保该方法返回保留值.但是,从非ARC使用此方法的程序员可能希望该方法返回一个自动释放的值(因为方法签名表明它返回了一个包含版权信息的字符串).因此他们不会在代码中释放该对象,从而导致内存泄漏.

On the other hand, if you compile this method with ARC, ARC sees that the name starts with copy, so it will make sure that the method returns a retained value. Programmers using this method from non-ARC however may expect the method to return an autoreleased value (as the method signature suggests that it returns a string containing copyright information). So they would not release the object in their code, resulting in a memory leak.

这篇关于使用第三个库代码时ARC如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:02