有没有可能让ARC忽略一个对象?

时间:2022-02-16 22:27:10

Recently converted an existing iPhone project to use ARC, and a portion of the code stopped working and kept crashing out. It seems that ARC was automatically releasing myRequest:

最近,将一个现有的iPhone项目转换为使用ARC,部分代码停止工作,继续崩溃。电弧似乎自动释放了我的请求:

ServerRequest *myRequest = [[ServerRequest alloc] init];

too soon, before some delegate functions had fired.

在一些委托函数被触发之前,还为时过早。

Now I know from reading up on ARC that you can set a compiler flag -fno-objc-arc against a specific file to ignore ARC. Now that worked fine, however, I'd still quite like to use ARC. I wonder if anyone knew of a way, when you instantiate an object, to tell ARC not to manage the releasing of it, e.g.:

现在我从阅读ARC上得知,您可以针对特定的文件设置一个编译器标记-fno- object - ARC来忽略ARC。不过,现在它工作得很好,我还是很喜欢使用ARC。当你实例化一个对象时,我想知道是否有人知道一种方法来告诉ARC不要管理它的释放,例如:

ServerRequest *myRequest = [[[ServerRequest alloc] init] ignoreARC];

3 个解决方案

#1


5  

Turning ARC off for a single variable? Not possible.

为单个变量关闭弧?不可能的。

You may want to try saving myRequest in an instance variable, so ARC knows you still need it. When you're done you can release it by setting it to nil.

您可能想尝试在实例变量中保存myRequest,这样ARC就知道您仍然需要它。完成后,可以将其设置为nil。

#2


4  

You can move variables outside of ARC by using __bridge casts to convert the object to a void*, but this not the solution to your problem. You have a memory management problem, and you need to hunt it down and fix it, not band-aid it.

通过使用__bridge强制转换将对象转换为void*,可以将变量移出ARC之外,但这并不是解决问题的方法。你有一个记忆管理的问题,你需要找到它并修复它,而不是权宜之计。

As @voidStern notes, if you have a request you care about keeping, you need to retain it, and then release it when you no longer care about it.

正如@voidStern指出的,如果您有一个请求需要保留,那么您需要保留它,然后在您不再关心它时释放它。

#3


4  

This is a really bad idea. ARC is releasing the object because the semantics of your code don't indicate that you want to keep it. You're trying to hide the symptom, not fix the problem.

这真是个坏主意。ARC释放对象是因为代码的语义不表明您想保留它。你试图掩盖症状,而不是解决问题。

Worse, if you managed to do what you want, you'd make your code really unmaintainable. Some variables being managed by ARC and some not sounds like a recipe for spending all your time hunting through code trying to figure out which is which.

更糟糕的是,如果您设法做了您想做的事情,那么您的代码就会变得非常不可维护。有些变量是由ARC管理的,有些则不是,这听起来像是在代码中寻找哪个是哪个。

If you create an object and you want to hang onto it beyond the current scope, put it into an instance variable or assign it to some other object's property.

如果您创建了一个对象,并希望将它挂在当前范围之外,那么将它放入实例变量中,或者将它分配给其他对象的属性。

#1


5  

Turning ARC off for a single variable? Not possible.

为单个变量关闭弧?不可能的。

You may want to try saving myRequest in an instance variable, so ARC knows you still need it. When you're done you can release it by setting it to nil.

您可能想尝试在实例变量中保存myRequest,这样ARC就知道您仍然需要它。完成后,可以将其设置为nil。

#2


4  

You can move variables outside of ARC by using __bridge casts to convert the object to a void*, but this not the solution to your problem. You have a memory management problem, and you need to hunt it down and fix it, not band-aid it.

通过使用__bridge强制转换将对象转换为void*,可以将变量移出ARC之外,但这并不是解决问题的方法。你有一个记忆管理的问题,你需要找到它并修复它,而不是权宜之计。

As @voidStern notes, if you have a request you care about keeping, you need to retain it, and then release it when you no longer care about it.

正如@voidStern指出的,如果您有一个请求需要保留,那么您需要保留它,然后在您不再关心它时释放它。

#3


4  

This is a really bad idea. ARC is releasing the object because the semantics of your code don't indicate that you want to keep it. You're trying to hide the symptom, not fix the problem.

这真是个坏主意。ARC释放对象是因为代码的语义不表明您想保留它。你试图掩盖症状,而不是解决问题。

Worse, if you managed to do what you want, you'd make your code really unmaintainable. Some variables being managed by ARC and some not sounds like a recipe for spending all your time hunting through code trying to figure out which is which.

更糟糕的是,如果您设法做了您想做的事情,那么您的代码就会变得非常不可维护。有些变量是由ARC管理的,有些则不是,这听起来像是在代码中寻找哪个是哪个。

If you create an object and you want to hang onto it beyond the current scope, put it into an instance variable or assign it to some other object's property.

如果您创建了一个对象,并希望将它挂在当前范围之外,那么将它放入实例变量中,或者将它分配给其他对象的属性。