【问题标题】:Memory Leak in iOS 5.1iOS 5.1 中的内存泄漏
【发布时间】:2012-03-28 03:58:51
【问题描述】:

我只是在UIViewController 中创建了一个简单的UITableView

.h

#import <UIKit/UIKit.h>

@interface RootController : UIViewController <UITableViewDataSource>
{
    NSMutableArray *_tableData;
    UITableView *_tableView;
}
@end

.m

#import "RootController.h"

@implementation RootController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        if (!_tableData) {
            _tableData = [[NSMutableArray alloc] initWithCapacity:20];
            for (int i = 0; i < 50; i++) {
                [_tableData addObject:[NSString stringWithFormat:@"%d", i]];
            }
        }
    }
    return self;
}

- (void)dealloc
{
    [_tableView release];
    [_tableData release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)loadView
{
    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = view;
    [view release];

    self.view.backgroundColor = [UIColor whiteColor];

    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
        _tableView.dataSource = self;
    }
    [self.view addSubview:_tableView];
}

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"TableViewCellIdentifier";
    UITableViewCell *cell = nil;
    cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    }

    cell.textLabel.text = [_tableData objectAtIndex:indexPath.row];

    return cell;
}

@end

当我滚动UITableView时,我使用仪器来检测内存泄漏,内存
发生泄漏。

我使用 Xcode 4.3.1 和 iOS5.1 (iPod touch4)。 有人遇到过这个问题吗?

【问题讨论】:

    标签: iphone objective-c uitableview memory-leaks instruments


    【解决方案1】:

    如果您查看“责任库”列,您会发现罪魁祸首不是您的代码。泄漏很小,您可以放心地忽略它们。

    这里有一个类似的问题:WebView: libdispatch leaks in an ARC-enabled app

    【讨论】:

    • @JesusRamos,如果我的程序运行几个小时,它可能会因为内存泄漏而被系统杀死。
    • @looyao 如果干扰应用程序正常运行,请报告为bug。
    • @JoshRagem Link 没有显示类似的问题。它显示相同的问题。
    • 现在更正,是同一个问题:P
    • 我也发现了同样的问题。iphonedevsdk.com/forum/iphone-sdk-development/…,所以我认为这是一个错误。
    猜你喜欢
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 2011-08-31
    相关资源
    最近更新 更多