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.
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@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
Hi Laurent, Thanks for the hint. Searched and found some docs that say the same thing. http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Garbag... Dave. On 29 Apr 2010, at 23:08, Laurent Sansonetti wrote:
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@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
participants (2)
-
Dave Baldwin
-
Laurent Sansonetti