On Feb 15, 2012, at 12:58 PM, Matt Neuburg <m...@tidbits.com> wrote: > This appears to be working from all points of view (thank you, Instruments!): > > MyMandelbrotOperation* op = > [[MyMandelbrotOperation alloc] initWithSize:self.bounds.size > center:center zoom:1]; > __block __weak id observer = [[NSNotificationCenter defaultCenter] > addObserverForName:@"MyMandelbrotOperationFinished" > object:op queue:[NSOperationQueue mainQueue] > usingBlock:^(NSNotification *note) { > MyMandelbrotOperation* op2 = note.object; > CGContextRef context = [op2 bitmapContext]; > if (self->bitmapContext) > CGContextRelease(self->bitmapContext); > self->bitmapContext = (CGContextRef) context; > CGContextRetain(self->bitmapContext); > [self setNeedsDisplay]; > [[NSNotificationCenter defaultCenter] removeObserver:observer > name:@"MyMandelbrotOperationFinished" object:op2]; > }]; > [self.queue addOperation:op]; > > This is delightful. I'm not leaking self, my operations are being dealloced > in good order, I'm successfully registering and deregistering, I'm > trampolining to the main thread without writing a trampoline method, I'm > avoiding the nightmare of storing my observers in an instance variable, op > and op2 are the same object - everything about it seems to be okay. My > questions are: > > * Is this really an okay way to talk?
Are you using GC or ARC? I think this is safe with GC but not safe with ARC, but I'm not an expert in the NSNotificationCenter machinery. > * Why was I crashing until I said __block? Without __block, the block object snapshots the value of the observer variable when the block object is created. But in your code the block object is created before the call to -addObserverForName:..., and at that point the observer variable is not yet initialized. Boom. With __block, the block object always uses the current value of the observer variable. The variable is initialized before the block object actually executes, so everything works. -- Greg Parker gpar...@apple.com Runtime Wrangler _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com