【问题标题】:why does iphone-xcode leak tool show a leak for this code?为什么 iphone-xcode 泄漏工具会显示此代码的泄漏?
【发布时间】:2010-10-25 17:15:05
【问题描述】:

我已经使用 build/analyze 完成了我的应用程序,并且能够解决所有问题。

但在泄漏工具下运行它会检测到泄漏并说它来自 tvc:viewDidLoad 其中 tvc 是一个 tableViewController

它进一步引用了 NSArray -> sectionlist

它显示了一个 malloc refct=1 然后保留 refct=2 然后发布 refct=1

下面是 viewDidLoad,后面是 dealloc 和 header

我看不出问题出在哪里?我错过了什么吗?

除了我包含的代码之外,sectionList 仅在其他 2 个地方引用 -

  1. in numberOfRowsInSection

    return [[self sectionList] count];

  2. 在 cellForRowAtIndexPath 中

    [[cell textLabel] setText:[[self sectionList] objectAtIndex:indexPath.row]];

这是一条红鲱鱼吗? IE;是真正的泄漏还是泄漏工具仅能够显示分配,但在稍后卸载 tvc 时无法显示释放在 dealloc 中发生的位置?

enter code here

/// code 
////////////////////////

// property
@synthesize sectionList = ivSectionList;

/*
*********************************************
build list of sections
*********************************************
*/
- (void)viewDidLoad
{
[super viewDidLoad];  

// alloc a local array and init with values  
NSArray *sections = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", nil];

// assign local array to tvc ivar  
[self setSectionList:sections];

// release local array  
[sections release], sections=nil;
}


/*
*********************************************
release ivSectionList
*********************************************
*/
- (void)dealloc
{
[ivsectionList release], ivsectionList=nil;
[super dealloc];
}


//////// header
////////////////////////////////////////
#import <UIKit/UIKit.h>

@interface tvc : UITableViewController
{
NSArray *ivsectionList;
}

@property (nonatomic, retain) NSArray *sectionList;
@end
//////////////////////////////////////// 

【问题讨论】:

    标签: xcode memory-leaks


    【解决方案1】:

    leaks(或 Instruments)告诉你泄露的对象是在哪里创建的

    很可能,客户没有发布它。

    一个子类也可能滥用这个类,最好让你的 ivars 默认为@private

    或者,您可能想尝试atomic 读/写,以防您在这种情况下遇到线程问题。

    (但是是的,摘录看起来是正确的)

    【讨论】:

    • 使用活动监视器我看到一个添加内存、释放模式,因为我在往返此 tvc - 15.13 - 15.23 - 16.42 - 15.28 - 15.14 的路径上反复工作 - 似乎表明真的没有泄漏。但经过一系列手术后我确实爆炸了,所以它可能说的是实话。将进一步调查并稍后发布结果。谢谢
    • 这是一个合法的泄漏 - 花了一段时间,但我找到了。泄漏发生在其他地方 - 创建了此类的一个实例但未发布。崩溃无关。
    • 酷。感谢您的跟进。系统框架中偶尔会出现泄漏,但这种情况不太常见。
    • @Mike wow 5 天发现泄漏......我现在 1 天后发现自己的泄漏并不太兴奋。 :o
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 2013-04-27
    • 2011-05-06
    • 2016-04-02
    • 1970-01-01
    相关资源
    最近更新 更多