[MacRuby-devel] How to stop observing

Laurent Sansonetti lsansonetti at apple.com
Thu Apr 29 15:08:13 PDT 2010


Hi Dave,

Normally, you would do this in -finalize, since -dealloc will not be called under GC.

Now, I believe (this should be confirmed...) that under GC, NSNotificationCenter & friends automatically unregister observers when they get collected. This is done through the weak reference machinery. It may not be implemented for all observer pattern-based APIs though, so in some cases doing it manually in -finalize might be required...

One critical thing to keep in mind while writing a -finalize method is to avoid resurrection. The GC does not support that. Make sure you do not insert "self" anywhere it could be "referenced" by a write barrier.

HTH,
Laurent

On Apr 29, 2010, at 11:33 AM, Dave Baldwin wrote:

> A common design pattern in cocoa seems to be to have a objc method along the lines:
> 
> - (void)dealloc {
> 
>     // Stop observing the tool palette.
>     [[NSNotificationCenter defaultCenter] removeObserver:self name:SKTSelectedToolDidChangeNotification object:[SKTToolPaletteController sharedToolPaletteController]];
> 
>     // Stop observing the document's canvas size.
>     [[self document] removeObserver:self forKeyPath:SKTDocumentCanvasSizeKey];
> 
>     // memory management stuff removed...
> }
> 
> - taken from the Sketch sample.
> 
> One way to handle this would be to add a finalizer method to act in place of the the dealloc, but is this the best or correct way to handle this?
> 
> Thanks,
> 
> Dave.
> 
> 
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel



More information about the MacRuby-devel mailing list