Bindings related NSUndoManager problem
Bindings related NSUndoManager problem
- Subject: Bindings related NSUndoManager problem
- From: Lawrence Manning <email@hidden>
- Date: Sat, 2 May 2009 12:28:37 +0100
Hi everyone,
First up, the usual disclaimer: I am new to cocoa, and Objective C,
etc. Please be gentle. :)
As a learning exercise I am writing a simple drawing program. It is
document based, and so far I have quite a few things working: loading/
saving, simple printing, drawing with lines, rectangles, ovals, etc.
My subclassed NSView holds an array of objects (subclassed
NSBezierPaths with attributes for color, etc), which are iterated
through and drawn to the screen via drawRect.
The UI consists of a few color selectors, radio buttons, checkboxes
placed in the document window. Each element is bound into the view
using the model key path:
myView.fgColor
etc. This works well: I can set the color of the next path added to
the view without any code. I can also set the default values for the
controls in the document from inside the NSView's initWithFrame, which
is also great as it means the defaults do not have be set in both IB
and the View code (which would be the case if I used actions instead
of a binding).
Now I have come to implement Undo. It actually works, but there is a
problem (see below). The code to implement Undo is in my view class:
- (void)removePath:(PathElement *)oldPath
{
NSUndoManager *undo = [[self window] undoManager];
[undo registerUndoWithTarget:self selector:@selector(addPath:)
object:oldPath];
[undo setActionName:@"Remove Object"];
[paths removeObject:oldPath];
[self setNeedsDisplay:YES];
}
- (void)addPath:(PathElement *)newPath
{
NSUndoManager *undo = [[self window] undoManager];
[undo registerUndoWithTarget:self selector:@selector(removePath:)
object:newPath];
[undo setActionName:@"Add Object"];
[paths addObject:newPath];
[self setNeedsDisplay:YES];
}
addPath is called via mouseUp, after setting up the PathElement.
removePath is currently only callable through the undo mechanism.
With this code, undo actually works. I can add and remove objects and
the Undo and Redo menu items work exactly as the should. The problem
comes when I close a document window. With a single binding set
(myView.filled) I get this in the console:
2009-05-02 11:53:55.595 Draw[38373:10b] An instance 0x16e4f0 of class
StretchView is being deallocated while key value observers are still
registered with it. Observation info is being leaked, and may even
become mistakenly attached to some other object. Set a breakpoint on
NSKVODeallocateBreak to stop here in the debugger. Here's the current
observation info:
<NSKeyValueObservationInfo 0x1780d0> (
<NSKeyValueObservance 0x163de0: Observer: 0x177e30, Key path: filled,
Options: <New: NO, Old: NO, Prior: NO> Context: 0x159300, Property:
0x1780b0>
)
[Session started at 2009-05-02 11:53:55 +0100.]
2009-05-02 11:53:55.596 Draw[38373:10b] StretchView dealloc
Loading program into debugger…
GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40
UTC 2008)
.....
When I comment out the undo-related code there is no error. When I
remove the binding there is no error. I am at a loss to work out how
to get past this problem, and was wondering if anyone could point me
in the right direction. I can put the full code online (its only 3
classes) if anyone is interested.
Cheers for any help,
Lawrence
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden