简体   繁体   English

iOS ARC强制保留NSURL

[英]ios ARC force retain NSURL

I am making a NSURL and passing it to a selector, which then passes it to another selector, etc. By the time it gets where it's going it logs just fine, but gives a sigabort when it's used. 我正在制作一个NSURL并将其传递给选择器,然后将其传递给另一个选择器,依此类推。当它到达要运行的位置时,它会记录得很好,但是在使用时会给出一个sigabort。 I suspect this means my object has been released by ARC. 我怀疑这意味着我的对象已被ARC释放。 How can I make sure it stays around long enough to get used? 我如何确保它可以使用足够长的时间?

__strong NSURL *url = [[NSURL alloc] initWithString:str];

... passes to a selector
... passes to another
... and then to fetchVideoContent

- (void)fetchVideoContent:(NSURL *)url withGUID:(NSString *)guid;
{
    NSMutableURLRequest *req;
    req = [NSMutableURLRequest requestWithURL:url // <-- DIES ON THIS LINE (SIGABRT)
                                  cachePolicy:NSURLRequestUseProtocolCachePolicy
                              timeoutInterval:30.0];
    ...

That's the "strongest" thing I could think of and that still doesn't work. 这是我能想到的“最强”的东西,但这仍然行不通。 Any advice? 有什么建议?

You need to ensure the initial url variable (__strong NSURL *url) continues to exist when the url object ends up at the fetchVideoContent method, if not, you'll get the error you're describing. 您需要确保当url对象最终到达fetchVideoContent方法时,初始url变量(__strong NSURL * url)继续存在,否则,您将得到所描述的错误。 Sounds to me like you're creating the url object in a method, using a local variable, and then passing that object through a few methods, that either cross to a new thread, or goes to the end of the runloop and back into the next run. 在我看来,您正在使用本地变量在方法中创建url对象,然后将该对象传递给一些方法,这些方法要么交叉到新线程,要么转到运行循环的末尾并返回到下一次运行。

For example, if through the steps you've omitted, the current run loop ends, and the initial url variable goes out of scope, the url object will be freed, since nothing is actually holding on to it anymore. 例如,如果通过省略的步骤,当前的运行循环结束,并且初始url变量超出范围,则url对象将被释放,因为实际上不再保留该对象了。 Passing the object to another method isn't enough to keep hold of it since no retain will be called on the parameter. 将对象传递给另一个方法不足以保留它,因为不会在参数上调用任何保留。

Short version is, make sure something holds onto url, you could make it a property of your class, an instance variable or even static if you'll only one instance of your class in use at a time. 简短的版本是,请确保某些内容可以保存在url上,如果您一次只使用一个类的实例,则可以使其成为类的属性,实例变量或什至是静态的。

First, you should verify that you are in fact dealing with a reference count issue -- run with zombies enabled. 首先,您应该验证自己实际上正在处理引用计数问题-在启用僵尸的情况下运行。

I've no idea what all the URL is being passed through, but there are corner cases where explicit reference counting is required when ARC is enabled. 我不知道所有URL都经过什么,但是在某些特殊情况下,启用ARC时需要显式引用计数。

If MRC semantics are needed, you can use CFRetain and match that with a CFRelease , or you can create your own functions which are not compiled with ARC enabled. 如果需要MRC语义,则可以使用CFRetain并将其与CFRelease匹配,或者可以创建自己的未启用ARC编译的函数。

Of course, you could simply use CFTypes instead (in this case). 当然,您可以简单地使用CFTypes(在这种情况下)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM