iPhone - NSUndoManager + NS 调用 +对象释放=崩溃

发布于 2024-11-10 10:29:41 字数 1060 浏览 1 评论 0原文

我正在为我的应用程序构建撤消/重做功能。我正在使用 NSUndoManager 的 NSInitation 方法。

这就是我构建调用的方式,

NSNumber *firstState = [NSNumber numberWithInt:fsNumber];
NSInvocation *initialState = [self restoreStateInvocation:firstState];
// ... the code continues...

这些是当UNDO/REDO调用initialState时相关的方法,

- (NSInvocation *) restoreStateInvocation:(NSNumber*)number {
  NSMethodSignature *executeMethodSignature = [self methodSignatureForSelector:
                                                 @selector(makeUNDO:)];
  NSInvocation *moveInvocation = [NSInvocation invocationWithMethodSignature: executeMethodSignature];
  [moveInvocation setTarget:self];
  [moveInvocation setSelector:@selector(makeUNDO:)];

  [moveInvocation setArgument:&number atIndex:2];
  return moveInvocation;
}


- (void) makeUNDO:(NSNumber*)number {

  int num = (int)[number intValue];
  // code crashes at this line... number appears to be deallocated at this time
  //
...
}

应用程序在makeUNDO的第一行崩溃,如代码所示。

如何保留号码而不泄露?

谢谢。

I am building a undo/redo functionality for my app. I am using the NSInvocation method of NSUndoManager.

This is how I build the invocation

NSNumber *firstState = [NSNumber numberWithInt:fsNumber];
NSInvocation *initialState = [self restoreStateInvocation:firstState];
// ... the code continues...

these are the methods relates

- (NSInvocation *) restoreStateInvocation:(NSNumber*)number {
  NSMethodSignature *executeMethodSignature = [self methodSignatureForSelector:
                                                 @selector(makeUNDO:)];
  NSInvocation *moveInvocation = [NSInvocation invocationWithMethodSignature: executeMethodSignature];
  [moveInvocation setTarget:self];
  [moveInvocation setSelector:@selector(makeUNDO:)];

  [moveInvocation setArgument:&number atIndex:2];
  return moveInvocation;
}


- (void) makeUNDO:(NSNumber*)number {

  int num = (int)[number intValue];
  // code crashes at this line... number appears to be deallocated at this time
  //
...
}

when the UNDO/REDO calls initialState the app crashes on the first line of makeUNDO, as pointed on the code.

how can I retain number without leaking?

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捎一片雪花 2024-11-17 10:29:41

正确的答案是将以下行添加到restoreStateInvocation...

[moveInvocation retainArguments];

the correct answer is to add the following line to restoreStateInvocation...

[moveInvocation retainArguments];
疯到世界奔溃 2024-11-17 10:29:41

您可以保留 NSNumber 对象,因为它是从 NSObject 继承的。

NSNumber* myNumber = [number retain];

使用完 myNumber 后,您还需要release

[myNumber 释放]

编辑:

使用下面描述的方法...

您可以将myNumber作为您的班级成员。

在 .h 中

   @interface myClass {
     NSNumber* myNumber;
     }
     ............
     ............
     @property(nonautomic,retain)  NSNumber* myNumber;

     @end

在实现文件(.m 文件)中。

@implementation myClass 
@synthesize myNumber;

-(void) dealloc{

   if(myNumber)
   {
       [myNumber release];
       myNumber  = nil ;
   }
}

- (void) makeUNDO:(NSNumber*)number {

  self.myNumber  = nil;
  self.myNumber  = number;


  int num = (int)[self.myNumberintValue];
  NSLog(@"My Number -->%d", num )

  //
  ...
}

you could retain NSNumber object because it's inherited from NSObject.

NSNumber* myNumber = [number retain];

you also need to release once you finished using myNumber.

[myNumber release].

EDITED:

Use the approach described below...

you could have myNumber as your class member.

in .h

   @interface myClass {
     NSNumber* myNumber;
     }
     ............
     ............
     @property(nonautomic,retain)  NSNumber* myNumber;

     @end

In implementation file (.m file).

@implementation myClass 
@synthesize myNumber;

-(void) dealloc{

   if(myNumber)
   {
       [myNumber release];
       myNumber  = nil ;
   }
}

- (void) makeUNDO:(NSNumber*)number {

  self.myNumber  = nil;
  self.myNumber  = number;


  int num = (int)[self.myNumberintValue];
  NSLog(@"My Number -->%d", num )

  //
  ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文