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