本文介绍了NSUndoManager是否保留对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我执行以下操作:

    Path2D *pathToRemove = [path copy];
    [[[self undoManager] prepareWithInvocationTarget:self] removePath:pathToRemove atIndex:index];
[pathToRemove autorelease];

我还有一个清除按钮,可以执行以下操作:

I also have a clear button that does:

[undoManager removeAllActions];

问题是removeAllActions使应用程序崩溃.当我删除[pathToRemove autorelease]时,它起作用了(或至少没有崩溃.这仍然可能是内存泄漏).我猜我以为undoManager在传递给'prepareWithInvocationTarget'调用时保留了'pathToRemove'.

Problem is that the removeAllActions crashes the app. When I removed the [pathToRemove autorelease], it worked (or at least didn't crash. It could still be a memory leak). I guess I was assuming that the undoManager retained 'pathToRemove' when passed in the 'prepareWithInvocationTarget' call.

不是这样吗?如果不是这种情况,那么崩溃可能会发生,因为对"removeAllActions"的调用释放了"PathToRemove"对象.但这意味着这是NSUndoManager中的错误,极不可能.

Is that not the case? If that is not the case then the crash could happen because the call to 'removeAllActions' is releasing the 'PathToRemove' object. But that would mean it is a bug in NSUndoManager which is highly unlikely.

我可以说我的copyWithZone实现不太可能是罪魁祸首,因为"[pathToRemove description]"和"[path description]"的NSLog输出显示的地址与预期的不同.

I can say that my copyWithZone implementation is not likely to be the culprit either since NSLog outputs for '[pathToRemove description]' and '[path description]' show different addresses as expected.

任何帮助将不胜感激.谢谢.

Any help would be appreciated. Thanks.

推荐答案

根据文档,prepareWithInvocationTarget:方法不保留传递给它的参数.从 NSUndoManager 文档,看来它只是捕获了 NSInvocation ,然后重播它.除非明确要求,否则NSInvocation对象不会在其参数中保留对象.

According to the documentation, the prepareWithInvocationTarget: method doesn't retain the arguments passed to it. From the NSUndoManager documentation, it appears that it simply captures the NSInvocation and later replays it. NSInvocationobjects don't retain the objects in their arguments unless specifically asked to do so.

这并不能完全解释崩溃的原因,因为removeAllActions只能清除撤消堆栈,而对对象不做任何操作.

That doesn't quite explain the crash, because removeAllActions is just supposed to clear the undo stack and not do anything to the objects.

希望这有助于某些人跟踪崩溃原因.

Hope this helps some in tracking down the source of the crash.

这篇关于NSUndoManager是否保留对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 06:10