【发布时间】: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