問題描述
XCode 4.6 顯示方法 + (void)beginAnimations:(NSString )animationID context:(void )context; 的內存洩漏警告 (XCode 4.6 shows a warning of memory leak for method + (void)beginAnimations:(NSString )animationID context:(void )context;)
This question here Iphone ‑ how to pass a parameter to animationDidStop? puts up the whole question in context. According to the best answer there, I was releasing the context in my animationDidStopSelector. But since I updated my Xcode, I am getting this warning
‑ (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];
}
And the warning in the log says:
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.
Is there a solution for this? And if not, how can I shut this warning for my project?
‑‑‑‑‑
參考解法
方法 1:
The problem is you are allocating an NSNumber inside this call. The number is not being released. Try changing this:
[UIView beginAnimations:nil context:[[NSNumber alloc] initWithFloat:self.view.frame.origin.x]];
Edit Here is a link to a post about handling the (void *)context
So you need some way to keep a reference to your NSNumber that can be cleaned later.
self.contextNumber = [[NSNumber alloc] initWithFloat:self.view.frame.origin.x];
[UIView beginAnimations:nil context:self.contextNumber];
and clean it in your dealloc
(by Ayush Goel、Jaybit)