XCode 4.6显示方法的内存泄漏警告+(void)beginAnimations:(NSString *)animationID context:(void *)context;

时间:2013-02-13 07:32:48

标签: ios objective-c xcode memory-leaks

这里的问题Iphone - how to pass a parameter to animationDidStop?在上下文中提出了整个问题。根据那里的最佳答案,我在animationDidStopSelector中发布了上下文。但是自从我更新了我的Xcode后,我收到了这个警告

 - (void) helloThere: (int) myValue {

  // I am trying to pass myValue to animationDidStop
  [UIView beginAnimations:nil context:[[NSNumber alloc] initWithFloat:self.view.frame.origin.x]]; //Warning raised on this line
  [UIView setAnimationDuration:1.0];
  [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
  [UIView setAnimationDelegate:self];

  // do stuff
  [UIView commitAnimations];
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

  CGFloat usesThisValue = [(NSNumber *) context floatValue];
  [(NSNumber *) context release];
}

日志中的警告说:

warning: Potential leak of an object       [UIView beginAnimations:nil context:[[NSNumber alloc] initWithFloat:self.view.frame.origin.x]]; //Warning raised on this line
 1 warning generated.

有解决方案吗?如果没有,我如何为我的项目关闭此警告?

1 个答案:

答案 0 :(得分:0)

问题是你在这个调用中分配一个NSNumber。该号码尚未发布。尝试更改此内容:

[UIView beginAnimations:nil context:[[NSNumber alloc] initWithFloat:self.view.frame.origin.x]];

修改 Here is a link to a post about handling the (void *)context

因此,您需要一些方法来保留对NSNumber的引用,以便稍后进行清理。

self.contextNumber = [[NSNumber alloc] initWithFloat:self.view.frame.origin.x];
[UIView beginAnimations:nil context:self.contextNumber]; 

并在你的dealloc

中清理它