使用 NSOperation 时的 EXEC_BAD_ACCESS

发布于 2024-08-04 03:28:27 字数 2020 浏览 0 评论 0原文

这与我遇到的问题几乎相同,除了代码非常不同: http://www.cocoabuilder.com/archive/message/cocoa/2009/3/24/233015

我想将一些处理卸载到 NSOperation,传递一个文件名作为 NSOperation 加载的引用,解析。输入 -(void)init 时,应用崩溃并显示 EXEC_BAD_ACCESS

以下是我启动操作的方式:

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;
}

在删除 NSOperation 中的所有内容后,我仍然以崩溃告终。以下代码崩溃:

#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

这是崩溃的汇编程序输出(我不是忍者,无法理解它所说的内容)。这发生在 __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]

。有什么想法吗? :)

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

I want to offload some processing to an NSOperation, passing a filename as a reference that the NSOperation loads and parses. The app crashes with EXEC_BAD_ACCESS when entering -(void)init.

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. 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

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? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小猫一只 2024-08-11 03:28:27

您应该在 -initWithContentsOfFile: 中调用 [super init];。 NSOperation 可能会在那里进行一些工作所需的设置。

You should be calling [super init]; in -initWithContentsOfFile:. NSOperation likely does some setup there that is required for it to work.

眼泪都笑了 2024-08-11 03:28:27

除了上面提到的缺少 [super init] 之外,您似乎也没有在 initWithContentsOfFile: 中保留 filename 。如果 filename 在其他地方释放并在操作执行之前释放,这可能会导致问题。

In addition to the lack of [super init] mentioned above, it doesn't look like you are retaining filename in initWithContentsOfFile:. This could cause problems if filename is released elsewhere and deallocated before the operation executes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文