views:

210

answers:

2

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

+3  A: 

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

kperryua
Haha, d'uh. Thanks :)
Antti
A: 

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.

Lawrence Johnston
Yeah i noticed this too. This slipped in when i was stripping out everything from the code, trying to find the problem.
Antti