2012-05-20 37 views
0

我在下面的代码中有泄漏的对象。我如何解决它?我试着添加一个[apiViewController发布];但是当我分析的应用程序,我仍然获得了:如何修复保留计数为+1的泄漏对象?

enter image description here

if (idx == 2) { 
     NSLog(@"you touched menu 2"); 

     APICallsViewController *apiViewController = [APICallsViewController alloc]; 
     [self.navigationController pushViewController:apiViewController animated:YES]; 
     //[apiViewController getFriendsCallAPIDialogFeed]; 
     [apiViewController getAppUsersFriendsUsing]; 


    } 

感谢您的帮助

+2

你不知道你在做什么。在iOS堆管理中找到一个很好的参考并研究它。 –

+0

其中一些内容已在[高级内存管理](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html)中介绍。 – Rob

回答

3

你就忘记了-init和-release。

APICallsViewController *apiViewController = [[APICallsViewController alloc] init]; 
... 
[apiViewController release]; 

您可能需要阅读Objective-C programming

0

对于每一个对象,你分配/保留,你是responcible释放。

[apiViewController getAppUsersFriendsUsing]; 

认沽...

[apiViewController release]; 
1

首先,你需要init,就像hwaxxer和Justin Boo建议的那样。

其次,有几个人建议使用autoreleaseautorelease是推迟的release,除非需要(例如,您的方法需要推迟发布,直到它可以将对象返回给调用方),否则通常不应这样做。因此,简而言之,只有在将对象返回给方法的调用方时才使用autorelease,否则使用release。在这种情况下,您应该使用release

在这个特定的场景中,这并不重要(因为通过推视图控制器,无论如何它都会被保留下来,直到视图从堆栈中弹出才会被释放),但是如果你打算做你自己的内存管理(即不使用ARC),在你将对象返回给方法的调用者并因此必须推迟release时,只要有可能(例如在这种情况下),只需要使用release即可,并且只有autorelease

第三,我建议阅读并确保您了解Advanced Memory Management。这给你一些你真正需要理解的基本内存管理规则(例如,如果你创建它,然后你拥有它,并且你必须释放它)。第四,一旦你掌握了内存管理的专业知识(并且只有在你做了之后,才能真正理解正在发生的事情),我会建议你认真考虑Transitioning to ARC,因为你不必处理太多这种愚蠢。