小编Tom*_*oka的帖子

iOS 5仅使用Release Build阻止崩溃

我已经使用了块和ARC,并且在某些情况下发现,iOS仅在Release版本中崩溃.编写代码是错误的方法,就像这样.

-(IBAction)clickedButtonA:(UIBarButtonItem*)sender event:(UIEvent*)event {
  NSMutableArray *arrRows = [NSMutableArray arrayWithCapacity:0];
  #warning this code only crash on Release Build.... Don't use this
  NSMutableDictionary * dicRow = [NSMutableDictionary dictionaryWithCapacity:0];
  [arrRows addObject:dicRow];
  dispatch_block_t block = ^{
    NSString *str = [NSString stringWithFormat:@"%@",[_tweet valueForKey:@"text"]];
    [[UIPasteboard generalPasteboard] setString:str];
  };
  [dicRow setValue:block forKey:kDicKeyLinkPopBlock];

  NSMutableArray *sections = [NSMutableArray arrayWithObject:arrRows];
  TOVLinkPopoverViewController *controller= [[TOVLinkPopoverViewController alloc] init];
  controller.arrayLink = sections;
}
Run Code Online (Sandbox Code Playgroud)

从其他控制器,当我访问该块时,它崩溃只有我在发布版本.我知道你需要复制块

[dicRow setValue:[block copy] forKey:kDicKeyLinkPopBlock];
Run Code Online (Sandbox Code Playgroud)

对于非块感知类,如NSMutableDictionary.

问题是"为什么它只会在发布版本上崩溃?" 我知道这应该"崩溃",这是使用块的错误方法,但希望它在Debug构建时崩溃,所以我们可以更早地找到这种bug.

还有一个问题是"是否存在任何使用调试版本使代码崩溃的构建设置?"

您可以从gitHub运行示例代码, https://github.com/tomohisa/iOS_PopoverMenu_Notification

请参阅ViewController.m并查找已注释掉的代码(仅在发布时崩溃).

ios objective-c-blocks automatic-ref-counting

1
推荐指数
1
解决办法
821
查看次数