将'NSError*const __strong*'发送到'NSError*__ autoreleasing*'类型的参数会更改指针的保留/释放属性

W.K*_*K.S 5 objective-c ios automatic-ref-counting

这个问题类似于ios NSError类型,但是那里描述的解决方案没有用,我相信它不是我需要的.

我有一个方法,它执行异步调用,然后调用完成块.当我尝试将NSError**传递给完成块时,我收到此错误:

将'NSError*const __strong*'发送到'NSError*__ autoreleasing*'类型的参数会更改指针的保留/释放属性

代码如下:

+(void) agentWithGUID:(NSString *) guid completion:(void (^)(AKAgentProfile * agentProfile, NSError ** error)) completionBlock
{
    dispatch_queue_t requestQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(requestQueue, ^{
        NSString * parameterizedUrl = [AKAgentProfileEndPoint stringByAppendingString:guid];
        NSURL *url = [NSURL URLWithString:parameterizedUrl];
        NSData *data = [NSData dataWithContentsOfURL:url];

        NSError * error = nil;

        AKAgentProfile * agentProfile = [[[AKAgentFactory alloc] init] agentProfileWithData:data error:&error];

        dispatch_async(dispatch_get_main_queue(), ^{
            completionBlock(agentProfile,&error);
        });

    });
}
Run Code Online (Sandbox Code Playgroud)

gna*_*729 6

你的完成块参数完全是胡说八道.

您在调用堆栈上有一个变量NSError*err.

然后尝试将err的地址传递给将在主线程中调用的完成块.当调用完成块时,您的函数已经很久才返回,而且错误是垃圾.如果完成块试图在那里存储任何东西,它会存储一个NSError*,其中很久以前你的错误变量在堆栈上,很可能会覆盖一些完全不相关的方法的一些有价值的数据.

这只适用于回调块.


Ole*_*ran 5

按值传递错误,而不是通过引用传递错误,即将块签名更改为void (^)(AKAgentProfile * agentProfile, NSError * error)并传递error而不是传递&error.