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;)


問題描述

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 GoelJaybit)

參考文件

  1. XCode 4.6 shows a warning of memory leak for method + (void)beginAnimations:(NSString )animationID context:(void )context; (CC BY‑SA 3.0/4.0)

#objective-c #iOS #XCode #memory-leaks






相關問題

為什麼 NSWindow 或 NSView 實例處理它自己的鍵事件,而不是它的委託? (Why does an NSWindow or NSView instance handle its own key events, and not its delegate?)

在調用之前釋放了 didFinishSelector ASIHTTPRequest 的自定義委託類 (Custom delegate class for didFinishSelector ASIHTTPRequest being deallocated before called)

通過 IKImage 視圖預覽圖像 (Previewg images via IKImage View)

Gaya Jendela Kustom di Kakao (Custom Window Style in Cocoa)

Тонкія белыя лініі дадаюцца пры абрэзцы выявы (Objective-C OSX) (Thin white lines being added when cropping an image (Objective-C OSX))

Obj-C:遞歸:在堆棧上創建新對像或使用可變性 (Obj-C: Recursion: Making new objects on stack or use mutability)

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;)

單元格中不可見的 UITextField 已將 userInteractionEnabled 設置為 No (UITextField in cell that is not visible has userInteractionEnabled set to No)

Gửi nhật ký sự cố ứng dụng / nhật ký ứng dụng thông thường (Send app crash logs/regular app logs)

我的鍵盤默認值僅在我的應用程序中更改 (My keyboard default be change in only my App)

在復選框上單擊獲取表行 ID - 目標 c (On check box click get the table Row ID - Objective c)

iPhone SDK:檢查 UITableView 中每個對象的 ID 號並相應地更改徽章號 (iPhone SDK: Checking the ID number for each object in a UITableView and change the badge number accordingly)







留言討論