【问题标题】:NSArray causing memory leak when simulated low memory warningNSArray 在模拟内存不足警告时导致内存泄漏
【发布时间】:2011-09-03 07:12:32
【问题描述】:

我制作了一个示例项目来重现此问题,其中包含两个视图:

根标头:

#import <UIKit/UIKit.h>
#import "view2.h"

@interface RootViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
    view2 *secondView;
    UITableView *table;
    NSArray *array;
}

@property (nonatomic, retain) view2 *secondView;
@property (nonatomic, retain) IBOutlet UITableView *table;
@property (nonatomic, retain) NSArray *array;
@end

根主目录:

#import "RootViewController.h"

@implementation RootViewController
@synthesize table, array, secondView;

- (void)viewDidLoad
{
    [super viewDidLoad];    
    if(self.array == nil){
        self.array = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
    }
}
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    table = nil;
    array = nil;
    secondView = nil;

}

- (void)dealloc
{
    [table release];
    [array release];
    [secondView release];
    [super dealloc];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array 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] autorelease];
    }

    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (secondView == nil) {
        secondView = [[view2 alloc] init];
    }
    [self.navigationController pushViewController:secondView animated:YES]; 
}
@end

view2 simple 包含一个带有文本“view 2”的标签,用于识别目的。 所有这些代码在根控制器中所做的就是创建一个值为 1、2、3、4 的数组,并将此文本作为行绑定到表中,单击任何行将视图 2 推送到堆栈上。 如果您使用泄漏工具工具在模拟器中加载应用程序,请单击任意行以显示 view2,然后模拟错误警告,出现以下泄漏: image 对于该行:

self.array = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];

这给我的主应用程序带来了很多问题,因为我正在使用数组在各处的表格中提供数据。

我尝试了各种方法来解决这个问题,例如以不同的方式声明数组无济于事。

非常感谢任何帮助!

谢谢

【问题讨论】:

    标签: iphone objective-c ios memory-leaks nsarray


    【解决方案1】:

    viewDidUnload 中,您混淆了属性与直接 ivar 访问。

    array = nil 只是将 ivar 设置为 nil 而不使用综合访问器方法。您必须使用点符号:self.array = nil;

    这样使用访问器setArray: 为您处理内存管理。

    混淆 ivars 和属性是 Objective-C 初学者经常遇到的问题。始终为属性和 ivars 使用不同的名称可以很容易地防止混淆:

    @synthesize array = _array;
    

    您可以在类的 @interface 中省略 ivar 声明,或将其命名为 @synthesize 指令中的名称。

    【讨论】:

    • 哦,哇,我不知道这会产生如此巨大的差异。我一直认为'self'关键字类似于c#中的'this',它只是用于区分变量的范围。不,我想我可能也是错的:S 非常感谢你,我花了 10 多个小时为此绞尽脑汁!
    猜你喜欢
    • 1970-01-01
    • 2013-01-20
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多