NSUndoManager does not retain the targets of undo operations, for several reasons. Foremost is that the client—the object performing undo operations—typically owns the NSUndoManager; thus for the NSUndoManager to retain the target would create cycles. The NSUndoManager does contain references to the targets of undo operations, however, which it uses to send undo messages when undo is performed. If a target object has been deallocated, and an undo message is sent to it, errors result.
To remedy this, the client must take care to clear undo operations for targets that are being deallocated. This typically occurs in one of three ways:
The client is the exclusive owner of the NSUndoManager
and the target of all undo operations. In this case the client can
simply release the NSUndoManager in its dealloc
method
or deconstructor.
The client shares the NSUndoManager with other clients. To
handle this the client should send removeAllActionsWithTarget:
(argument
of self
) to the NSUndoManager before releasing
it in its dealloc
method.
The client registers objects other than itself for undo operations.
Here either the client must watch for the other objects being deallocated
in order to send removeAllActionsWithTarget:
,
or the other objects must do so themselves when deallocated (which
requires that they have a reference to the NSUndoManager). This
is likely to be needed with invocation-based undo.
In a more general sense, it sometimes makes sense to clear
all undo and redo operations. Some applications might want to do
this when saving a document, for example. To this end, NSUndoManager
defines the removeAllActions
method, which
clears both stacks.
© 2002 Apple Computer, Inc. All Rights Reserved. (Last updated: 2002-11-12)