EXEC_BAD_ACCESS when using NSOperation

This is almost the same problem as mine, except with very different code: http://www.cocoabuilder.com/archive/message/cocoa/2009/3/24/233015

I want to offload some NSOperation processing by passing the filename as a reference that NSOperation downloads and parses. When you log in, the -(void)init

application crashes with EXEC_BAD_ACCESS


This is how I run 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 deleting everything I have in my NSOperation, I still get wrecked. The following code will work:

#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!



Here's the assembler output to fail (I'm not a ninja enough to understand what it says). This happens right 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? :)


source to share

2 answers

You must call [super init];

in -initWithContentsOfFile:

. NSOperation is probably doing some customization needed to get it working.



In addition to the lacks [super init]

mentioned above, it doesn't look like you are saving filename

to initWithContentsOfFile:

. This can cause problems if filename

freed elsewhere and freed before the operation is performed.



All Articles