UITableView中的内存泄漏与NSMutableArray - 如何阻止它们?

时间:2011-01-19 17:42:17

标签: iphone objective-c memory-leaks

我对Objective-c开发很陌生,而且我已经开始测试我的应用程序是否存在泄漏并修补了我本来可能做错的其他事情。我按照我买的一本书中的例子,并扩展了这些想法。泄漏工具告诉我在我的tableView cellForRowAtIndexPath方法我有泄漏,我不知道如何解决它。

以下是相关的.h内容:

@interface NewsListViewController : UITableViewController<UIActionSheetDelegate> {
NSMutableArray *newsList, *account, *playerList;}

这是相关的.m内容:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)ip {
static NSString *CellIdentifier = @"NewsCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                  reuseIdentifier:CellIdentifier];
    [cell autorelease];
}

NSManagedObject *uNews = [newsList objectAtIndex:[ip row]];

NSManagedObjectContext *playerDBContext = [[AppController sharedAppController] managedObjectContext];

NSFetchRequest *playerDBRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *playerDBEntity = [NSEntityDescription entityForName:@"Players"
                                                  inManagedObjectContext:playerDBContext];
[playerDBRequest setEntity:playerDBEntity];

NSPredicate *playerDBPredicate = [NSPredicate predicateWithFormat:@"playerID=%@", [uNews valueForKey:@"playerID"]];
[playerDBRequest setPredicate:playerDBPredicate];

NSError *playerDBError;

NSArray *playerDBList = [playerDBContext executeFetchRequest:playerDBRequest error:&playerDBError];

[playerDBRequest release];

playerList = [playerDBList mutableCopy];

NSString *playerInformation;

if (![playerDBList count] == 0) {
        NSManagedObject *playerInfo = [playerList objectAtIndex:0];
        playerInformation = [NSString stringWithFormat:@"%@, %@ (%@-%@)", [playerInfo valueForKey:@"playerLastName"],
                                                                          [playerInfo valueForKey:@"playerFirstName"],
                                                                          [playerInfo valueForKey:@"team"],
                                                                          [playerInfo valueForKey:@"position"]];
} else {
    //NSInteger playerID = (NSInteger *)[uNews valueForKey:@"playerID"];
    [self addPlayer:(NSInteger *)[uNews valueForKey:@"playerID"]];
    NSLog(@"%@", [uNews valueForKey:@"playerID"]);
    playerInformation = [uNews valueForKey:@"playerInfo"];
}

cell.textLabel.text = playerInformation;
cell.detailTextLabel.text = [uNews valueForKey:@"news"];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;}

它在playerList = [playerDBList mutableCopy]上抛出错误; line - 非常感谢帮助解决方法和解释。它可能来自重新分配而不释放,但是当我尝试使用[playerList release]时;在cellForRowAtIndexPath的末尾我的应用程序崩溃了。

2 个答案:

答案 0 :(得分:2)

属性会使这'正常工作'。

·H:

...
@property (nonatomic, retain) NSMutableArray *playerList;
...       

的.m:

@implementation MyClass 

@synthesize playerList;

... then in your cellForIndexPath method ...

    self.playerList = [[playerDBList mutableCopy] autorelease];

...

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

声明为'retain'的属性将在分配属性时自动处理内存管理,如果存在,则在保留新值之前释放旧值。

答案 1 :(得分:1)

您尝试的版本崩溃了,因为第一次通过播放器列表尚未分配并且您发布了nil。但第二次通过它有一些东西,你泄漏它。每当我重复使用这样的保留指针时,我都会

if( playerList )
   [playerList release];
playerList = [playerDBList mutableCopy];

只是为了安全。