我有一个简单的应用程序内存泄漏问题。该代码取自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。你知道泄漏的位置吗?
您是否在使用ARC(自动引用计数)? – Max 2012-03-18 13:20:36
你在评论中说过你正在使用ARC,在这种情况下很难泄漏内存。你怎么知道你有泄漏?泄漏的大小通常会增加,而不是固定的大小。 – jrturton 2012-03-18 18:33:19
我使用仪器检查泄漏。当我尝试滚动时,仪器显示泄漏48k。我真的不知道泄漏的原因是什么。当我在我的项目中发现完全相同的情况时,我开始玩这个简单的例子。我正在等待Aple Developer程序的接受,希望能尽快检查iPhone上的代码。 – Wojtek 2012-03-18 23:49:38