2012-03-18 69 views
0

我有一个简单的应用程序内存泄漏问题。该代码取自iPhone iOS Development Essentials一书。的代码如下:简单的UITableView内存泄漏

为H文件

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 
<UITableViewDelegate, UITableViewDataSource> 

@property (strong, nonatomic) NSArray *colorNames; 
@end 

及m文件

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

@synthesize colorNames; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.colorNames = [[NSArray alloc] initWithObjects:@"blue", @"red",@"green",@"yellow", nil]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    self.colorNames = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
    } else { 
     return YES; 
    } 
} 
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.colorNames count]; 
} 

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell==nil) 
    { 
     cell = [[UITableViewCell alloc] 
       initWithStyle:UITableViewCellStyleDefault 
       reuseIdentifier:CellIdentifier]; 
    } 
    cell.textLabel.text = [self.colorNames objectAtIndex:[indexPath row]]; 
    return cell; 
} 

@end 

当我尝试使用iPhone模拟器我有内存泄漏滚动表格每次48K。你知道泄漏的位置吗?

+0

您是否在使用ARC(自动引用计数)? – Max 2012-03-18 13:20:36

+0

你在评论中说过你正在使用ARC,在这种情况下很难泄漏内存。你怎么知道你有泄漏?泄漏的大小通常会增加,而不是固定的大小。 – jrturton 2012-03-18 18:33:19

+0

我使用仪器检查泄漏。当我尝试滚动时,仪器显示泄漏48k。我真的不知道泄漏的原因是什么。当我在我的项目中发现完全相同的情况时,我开始玩这个简单的例子。我正在等待Aple Developer程序的接受,希望能尽快检查iPhone上的代码。 – Wojtek 2012-03-18 23:49:38

回答

1

而不是@synthesizecolorNames你应该使用:

@synthesize colorNames = _colorNames; 

这将创建一个伊娃名字_colorNames

现在用途:

_colorNames = [[NSArray alloc] initWithObjects:@"blue", @"red",@"green",@"yellow", nil]; 

使用self.colorNames = [[NSArray ...的问题是,你的财产得到colorNames双保留。一旦通过你属性的属性(强),并通过调用'alloc'一次。

viewDidUnload你应该使用:

[_colorNames release]; 
_colorNames = nil; 
+1

现有的'viewDidUnload是好的,已经做了你通过使用setter的建议 – 2012-03-18 13:31:12

+0

我只是试图修改代码,并没有使用Arc。结果是相同的,当我尝试滚动UITableView时出现48k泄漏。 再次感谢您的帮助。 Wojtek – Wojtek 2012-03-18 15:31:43

2

假设你不使用ARC仅

如果@propertycolorNames是保留一个你需要,例如做

NSArray* cArray = [[NSArray alloc] initWithObjects:@"blue", @"red",@"green",@"yellow", nil]; 
self.colorNames = cArray; 
[cArray release]; 

此外autorelease您的手机一旦创建。

if(cell==nil) 
{ 
    cell = [[[UITableViewCell alloc] 
       initWithStyle:UITableViewCellStyleDefault 
       reuseIdentifier:CellIdentifier] autorelease]; 
} 

编辑如果你点击此内存泄漏,仪器能带给你创建一个泄漏的代码的特定行。

希望它有帮助。

0

浏览不同的论坛我找到了答案(我希望)。由于漏洞来自lib system_c.dlib和负责框架strdup,所以人们声称它是Apple库中的一个包。在UIPickerView,UIDatePicker和UIScrollView控制器中发现了同样的问题。相同的大小(48字节),相同的库和相同的框架。

1

我有同样的问题,已经发布了一个错误报告。 支持人员回答我说他们知道问题,Bug仍处于打开状态,ID为#10703036。

即使在Xcode的4.3.2更新之后仍在等待...