简体   繁体   English

使用NSOperation时为EXEC_BAD_ACCESS

[英]EXEC_BAD_ACCESS when using NSOperation

This is pretty much the same problem i have, except with very different code: http://www.cocoabuilder.com/archive/message/cocoa/2009/3/24/233015 除了几乎不同的代码外,这几乎与我遇到的问题相同: http : //www.cocoabuilder.com/archive/message/cocoa/2009/3/24/233015

I want to offload some processing to an NSOperation, passing a filename as a reference that the NSOperation loads and parses. 我想将一些处理工作卸载到NSOperation,并传递文件名作为NSOperation加载和解析的参考。 The app crashes with EXEC_BAD_ACCESS when entering -(void)init . 输入-(void)init时,应用程序崩溃并显示EXEC_BAD_ACCESS

Here's how i'm launching the operations: 这是我启动操作的方式:

int n = [files count];
for (int i = 0; i < n; i++) {
    NSString *filename = [files objectAtIndex:i];
    FilterParseOperation *parser = [[FilterParseOperation alloc] initWithContentsOfFile:filename];
    [filterParseQueue addOperation:parser];
    [parser release], parser = nil;
}

After stripping out everything i have in my NSOperation i still end up with a crash. 除去NSOperation中的所有内容后,我仍然会崩溃。 The following code crashes: 以下代码崩溃:

#import "FilterParseOperation.h"

@implementation FilterParseOperation

- (id)initWithContentsOfFile:(NSString *)aFilename {
    filename = aFilename;
    return self;
}

- (void)dealloc {
    [filename release], filename = nil;
    [super dealloc];
}

- (void)main {
    // do nothing!
}

@end

Here's the assembler output for the crash (i'm not ninja enough to understand what it says). 这是崩溃的汇编输出(我对忍者的理解不足以理解它的意思)。 This happens straight after addOperation in __opLock 这在__opLock中的addOperation之后立即发生

0x305ce610  <+0000>  push   ebp
0x305ce611  <+0001>  mov    ebp,esp
0x305ce613  <+0003>  push   ebx
0x305ce614  <+0004>  sub    esp,0x14
0x305ce617  <+0007>  call   0x305ce61c <__opLock+12>
0x305ce61c  <+0012>  pop    ebx
0x305ce61d  <+0013>  mov    eax,DWORD PTR [eax+0x4]
0x305ce620  <+0016>  mov    edx,DWORD PTR [eax+0x14] <- Crash happens here
0x305ce623  <+0019>  mov    eax,DWORD PTR [ebx+0xbfe94]
0x305ce629  <+0025>  mov    DWORD PTR [esp+0x4],eax
0x305ce62d  <+0029>  mov    DWORD PTR [esp],edx
0x305ce630  <+0032>  call   0x306af856 <dyld_stub_objc_msgSend>
0x305ce635  <+0037>  add    esp,0x14
0x305ce638  <+0040>  pop    ebx
0x305ce639  <+0041>  leave  
0x305ce63a  <+0042>  ret    
0x305ce63b  <+0043>  nop    DWORD PTR [eax+eax+0x0]

Any ideas? 有任何想法吗? :) :)

You should be calling [super init]; 您应该调用[super init]; in -initWithContentsOfFile: . -initWithContentsOfFile: NSOperation likely does some setup there that is required for it to work. NSOperation可能会在那里进行一些必要的设置才能使其正常运行。

In addition to the lack of [super init] mentioned above, it doesn't look like you are retaining filename in initWithContentsOfFile: . 除了缺少上述[super init]之外,您似乎并没有在initWithContentsOfFile:中保留filename This could cause problems if filename is released elsewhere and deallocated before the operation executes. 如果在执行操作之前将filename释放到其他位置并释放,则可能导致问题。

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

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