2

I have an NSOperation that I wanted to bracket with an NSAutoreleasePool, like this:

- (void) start {

    opPool = [[NSAutoreleasePool alloc] init];

    if (self.isCancelled) {
        [self finish];
    }
    ''' more code ...   
}

and

- (void) finish {

    [opPool release];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextDidSaveNotification object:nil];
    ... more finish code here ...
    self.state = LSOperationStateFinished;
}

opPool is an iVar.

The operation works without the NSAutoreleasePool allocation and release. But it crashes with it implemented as shown.

I put a break point in both methods to see what the description of [NSThread currentThread] is. It is the same inside both methods.

My understanding from the documentation on Autorelease Pools is that I should set one up for background threads that is separate from the main thread. This was how I wanted to do it, but apparently this is not the right way?

Am I doing something fundamentally wrong? What should I do to satisfy the documented requirement?

Jim
  • 5,940
  • 9
  • 44
  • 91

2 Answers2

0

If you have a crash, post the backtrace.

As it is, there isn't enough information to say much of anything specific.

In any case, you are likely over-releasing something. Turn on zombies and see if that helps.

bbum
  • 162,346
  • 23
  • 271
  • 359
  • So are you able to confirm that I'm not doing something fundamentally wrong with the code I've shown? If not, then I suppose I may have mistakenly released an autoreleased object. I am not seeing any zombies in the debugger or in instruments. It just crashes in the secondary thread of the NSOperation.. – Jim Feb 28 '12 at 04:30
  • I should add that it crashes exactly at the release. I can run to a break point there, and it crashes on the first single step. – Jim Feb 28 '12 at 04:33
0

You can insert your own NSAutoreleasePool to main thread, that's okay.

HERE is a thread about it.

I think the main reason that caused your crash is [opPool release];. Have you tried [opPool drain]; instead of [opPool release];?


EDIT:

It seems you declared your opPool as iVar, so have you released it in dealloc? If so, you got two release and crashed as a result.

Maybe you can try something like:

...
NSAutoreleasePool * opPool = [[NSAutoreleasePool alloc] init];

[self start];
[self finish];

[opPool drain];
...
Community
  • 1
  • 1
Kjuly
  • 34,476
  • 22
  • 104
  • 118
  • The only release is in the finish method. But at that point it should release all of the objects the pool owns, so I'm guessing something in the pool is being over-released. I don't know if it's okay to initialize and drain/release the pool in main thread. That's why I have it in start and finish. dealloc runs in the main thread. – Jim Feb 28 '12 at 06:23
  • @Jim Ya, it might was over-released. "_I don't know if it's okay to initialize and drain/release the pool in main thread_" that's okay as I said, and you can see the thread I pasted in the post, they had discussed about it. :) – Kjuly Feb 28 '12 at 10:44