【问题标题】:Simple UITableView memory leak简单的 UITableView 内存泄漏
【发布时间】:2012-04-03 05:59:13
【问题描述】:

我在一个简单的应用程序中遇到了内存泄漏问题。代码取自一本书 iPhone iOS Development Essentials。代码如下:

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 的内存泄漏。你知道泄漏在哪里吗?

【问题讨论】:

  • 您在使用 ARC(自动引用计数)吗?
  • 您在评论中说过您正在使用 ARC,在这种情况下很难泄漏内存。你怎么知道你有泄漏?泄漏的大小通常会增加,而不是固定大小。
  • 我使用仪器检查泄漏。当我尝试滚动时,仪器一直显示泄漏 48k。我真的不知道泄漏的原因是什么。当我在我的项目中发现完全相同的情况时,我开始使用这个简单的示例。我正在等待 Aple Developer 计划的接受,因此希望我能尽快在 iPhone 上检查代码。

标签: ios uitableview memory memory-leaks


【解决方案1】:

我有同样的问题,已经发布了错误报告。 支持人员回答我说他们已经意识到了这个问题,Bug 仍然处于打开状态,ID 是#10703036。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      你应该使用 @synthesize colorNames 而不是:

      @synthesize colorNames = _colorNames;
      

      这会创建一个 ivar 名称 _colorNames

      现在使用:

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

      使用self.colorNames = [[NSArray ... 的问题是您的属性colorNames 被双重保留。一次是你的属性的属性(强),一次是调用'alloc'。

      viewDidUnload 你应该使用:

      [_colorNames release];
      _colorNames = nil;
      

      【讨论】:

      • 现有的 `viewDidUnload 很好,并且已经通过使用 setter 完成了您的建议
      • 我只是尝试修改一个代码,并没有使用 Arc。结果是一样的,当我尝试滚动 UITableView 时,48k 泄漏。再次感谢您的帮助。沃伊泰克
      【解决方案4】:

      假设你不使用 ARC

      仅当 @property colorNames 是您需要做的保留时

      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];
      }
      

      编辑如果您单击该内存泄漏,仪器可以将您带到造成泄漏的特定代码行。

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 2013-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-09
        • 2013-11-06
        • 2011-06-03
        • 1970-01-01
        相关资源
        最近更新 更多