EXC_BAD_ACCESS 块执行时

EXC_BAD_ACCESS upon block execution

我有一个只有一个方法的 class,它使用 URLConnection 将序列化的 NSDictionary 发送到某个 URL 处的脚本,然后调用完成块.这是该方法的代码:

- (void)sendDictionary:(NSDictionary *)dictionary toScript:(NSString *)scriptName completion:(void (^) (id response))completionBlock
{

    ...Serialize data and add it to an NSURLRequest request...

    H2URLConnection *connection = [[H2URLConnection alloc]initWithRequest:request];

    //Define a semaphore to block execution of later statements until the signal is received.
    dispatch_semaphore_t sem = dispatch_semaphore_create(0);

    [connection setCompletionBlock:[^(id obj, NSError *err)
     {
         if (!err) {
            //Catch the server response
             NSString *receivedString = [[NSString alloc] initWithData:obj encoding:NSUTF8StringEncoding];
             NSLog( @"ChecklistAppNetworkManager received string: %@", receivedString);

             //Convert the JSON response into an NSDictionary
             NSError *otherError;
             id deserializedJSON = [NSJSONSerialization JSONObjectWithData:obj options:kNilOptions error:&otherError];

             if (otherError) {
                 NSLog(@"ChecklistAppNetworkManager JSON Error: %@", otherError.description);
             }

             [completionBlock invoke];

             NSLog(@"ChecklistAppNetworkManager JSON Response: %@", deserializedJSON);

             //Dispatch the semaphore signal so that the main thread continues.
             dispatch_semaphore_signal(sem);

         } else {
             NSLog(@"ChecklistAppNetworkManager encountered an error connecting to the server: %@", [err description]);
         }

     }copy]];
    //Finalize and initate the connection.
    [connection start];

    //Since block is dispatched to main queue, stall with a loop until the semaphore signal arrives.
    while (dispatch_semaphore_wait(sem, DISPATCH_TIME_NOW)) {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
    }
}

我正在尝试从另一个 class 中的此 class 的实例调用此方法,其中定义了完成块。这是我获得 EXC_BAD_ACCESS:

的代码
- (void)doSomeServerTask
{
    H2ChecklistAppNetworkManager *currentNetworkManager = ((H2AppDelegate *)[[UIApplication sharedApplication]delegate]).networkManager; //Instantiate class where that method is defined

    NSMutableDictionary *dictonary = [NSMutableDictionary dictionary];

    ...populate dictionary...

    [currentNetworkManager sendDictionary:dictionary toScript:@"script.php" completion:[^(id response)
     { //THIS iS THE LINE WHERE THE BAD ACCESS OCCURS

         NSLog(@"LoginViewController received response: %@", response);

     } copy]];
}

如有任何帮助,我们将不胜感激!

该方法的 completionBlock 接受一个参数,但您使用 invoke 方法调用该块。更有可能的是,崩溃是因为运行时试图保留内存中应该是该参数的任何垃圾。


但是,您确实需要完全重构此代码。阻塞主事件循环是不好的。 运行 子运行循环在 MEL 上更糟糕;它改变了调度队列处理语义的工作方式,并可能导致病态的不良性能或行为。

您应该转向真正的异步模型。如果在完成这些查询之前应用程序无法继续,则设置一个阻止进度的模态指示器。

为此,您将代码松散地构造为:

• 将用户界面置于 "loading..." 或其他模式状态

• 使用完成处理程序执行异步数据请求

• 在完成处理程序中,将 "update UI" 请求分派到主队列

• 在 "update UI" 后,拆除您的模式 "loading...." UI 并为用户更新显示

无需阻塞主事件循环即可执行任何操作。