简体   繁体   English

使用Swift返回的块调用Objective-C方法会导致EXC_BAD_ACCESS

[英]Calling Objective-C method with a returned block from Swift causes EXC_BAD_ACCESS

From a Singleton, I try to call the following code, and I get the crash when exiting the request() method: 我从Singleton尝试调用以下代码,退出request()方法时出现崩溃:

EXC_BAD_ACCESS The stack ends with a swift_unknownRelease at the top. EXC_BAD_ACCESS堆栈在顶部以swift_unknownRelease结尾。

let userService = UserService()

userService.request(user, data: data) { (dict:[NSObject : AnyObject]!, error:NSError!) in
  if let err = error {
    log.error("Add, delete or modified request: \(err)")
  }
}

the obj-c method is: obj-c方法是:

- (void)request:(UserEntity *)userEntity data:(MyData *)data withCompletion:(void(^)(NSDictionary *dict, NSError *error))completion
{
  NSString *url = [NSString stringWithFormat:@"%@?Id=%@&ype=%@",k_SERVER_REQUES, userEntity.contactID, data];

  NSOperation *reqOp = [self requestOperationWithMethod:@"POST"
                                                       URL:url
                                             withParameter:nil
                                                   success:^(NSInteger status, NSString *message, id data) {
    NSError *error;

    if (status == 1)
    { //If no error
      User *user = [userEntity fetchUserWithoutOverwriting];
      [[NSNotificationCenter defaultCenter] postNotificationName:kNotifDataChanged object:friendModel];
    }else{
      error = [NSError errorWithDomain:@"data" code:-1 userInfo:@{@"status":[NSNumber numberWithInteger:status]}];
    }
    if (completion) completion(data, error);

  } failed:^(NSString* s) {
    if(completion) completion(nil, [NSError errorWithDomain:@"data" code:-1 userInfo:@{@"detail":s}]);
  }];

  NSOperation *refreshOp = [[FeedService new] refreshProfileOperation:userEntity.contactID.longLongValue];

  // We are using NSOperation and dependency to ensure that refreshOp is executed before followOp.
  // So that the changes in followOp doesn't get overwritten by the refreshOp

  [reqOp addDependency:refreshOp];
  [[[AFHTTPRequestOperationManager manager] operationQueue] addOperation:refreshOp];
  [[[AFHTTPRequestOperationManager manager] operationQueue] addOperation:reqOp];
}

It's working 99% of the time. 99%的时间都在工作。 but I have a user that triggers a systematic crash and I don't know why because it seems to be identical. 但我有一个触发系统崩溃的用户,但我不知道为什么,因为它似乎是相同的。

First of all, if I change the call to: 首先,如果我将呼叫更改为:

userService.request(user, data: data, withCompletion: nil) 

Then it never crashes. 然后它永远不会崩溃。 So the crash is actually happening when exiting the completion block. 因此,崩溃实际上是在退出完成模块时发生的。

The values of the parameters when entering the block are: 进入块时的参数值为:

dict    [NSObject : AnyObject]! Some
error   NSError!    nil None

I wonder why this is happening and also why it happens only with this specific user... 我想知道为什么会发生这种情况,以及为什么只有这个特定用户才会发生这种情况...

In the Objective-C method at this line: 在此行的Objective-C方法中:

failed:^(NSString* s) {
    if(completion) completion(nil, [NSError errorWithDomain:@"data" code:-1 userInfo:@{@"detail":s}]);
}

where you can provide an empty Dictionary and not nil 您可以在其中提供空Dictionary而不是nil

The reason was: The server sometimes doesn't send back a Dictionary but a String... I guess I should use mantle or something like that in order to parse requests results. 原因是:服务器有时不发送字典而是返回字符串...我想我应该使用披风或类似的东西来解析请求结果。 The crash is not very explicit and using Address Sanitizer was not helping me debugging the issue. 崩溃不是很明显,使用Address Sanitizer并不能帮助我调试问题。

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

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