【问题标题】:NSOperation subclass release make Instruments crashNSOperation 子类发布使 Instruments 崩溃
【发布时间】:2013-01-14 23:33:49
【问题描述】:

我在Xcode 4.5 的 Mac OS X 应用程序上运行了仪器。我有两个 NSOperation 依赖子类,我在将它们添加到进程队列后忘记释放它们。所以我在将它们添加到队列后就释放了它们。该应用程序运行良好。我在 Instruments 上对其进行了分析,但它崩溃了。

processQueue = [[NSOperationQueue alloc] init];
NSUInteger max = [[NSUserDefaults standardUserDefaults] integerForKey:@"jobsKey"];
processQueue.maxConcurrentOperationCount = max;
GeocacheDownloadOperation * downloadOp = [[GeocacheDownloadOperation alloc]  initWithGeocache:cache InPath:directoryPath withDelegate:self];        
GeocacheJPGConversionOperation * conversionOp = [[GeocacheJPGConversionOperation alloc] initWithCache:cache WithPath:directoryPath WithDelegate:self];

[conversionOp addDependency:downloadOp];     
[processQueue addOperation:downloadOp];
[processQueue addOperation:conversionOp];

[downloadOp release];
[conversionOp release]; //This line makes Instruments crash

Instruments 在我想释放最后一个操作时崩溃(参见代码),但应用程序似乎运行良好。

有人有建议吗?是 Instruments 的错误还是我的代码有问题?

【问题讨论】:

  • 您怎么知道是您的应用程序导致仪器崩溃?
  • 当我在发布的代码中注释最后一行时,Instruments 运行良好。它因为分段错误而崩溃。
  • 可能是conversionOp类里面的东西,你覆盖dealloc吗?
  • 我重写了 dealloc 并且只释放了一个字符串对象。但它不应该改变任何东西。当您在进程队列中添加一个操作时,它会被保留。并且该应用仅在 Intruments 中崩溃。

标签: objective-c crash instruments nsoperation


【解决方案1】:

我的猜测是,conversionOp 在解除分配时会释放所有依赖项(在这种情况下是 downloadOp)。所以你在两个操作都完成后调用 [conversionOp release](这取决于线程的调度方式),所以你过度释放了 downloadOp。

【讨论】:

  • 我尝试只删除[downloadOp release]。该应用程序仍然在 Instruments 中崩溃。然后我删除了第二个版本,Instruments 工作但我有 GeocacheDownloadOperationGeocacheJPGConversionOperation 泄漏...
  • 你还能展示 GeocacheDownloadOperation 和 GeocacheJPGConversionOperation 类吗?
  • 对不起,我不允许发布它:(
【解决方案2】:

我发现了错误,但我无法解释为什么它单独工作而不是在 Instruments 中工作。我在NSOperation 子类中使用了一个已发布变量,这是我在 NSOperation 子类的dealloc 函数中第二次发布的。现在,我不再在 NSOperationsubclasses 中覆盖 [super dealloc] 并且它可以工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    相关资源
    最近更新 更多