简体   繁体   English

仪器显示[NSBundle mainBundle] loadNibNamed上的内存泄漏

[英]Instrument show memory leak on [NSBundle mainBundle] loadNibNamed

Friends, 朋友们,

I run my code in instruments, it show memory leak in 5 line(out of following code) ie cell = [[[NSBundle mainBundle] loadNibNamed:@"ZoomCustomVideoCell" owner:self options:nil] objectAtIndex:0]; 我在仪器中运行我的代码,它在第5行中显示了内存泄漏(以下代码之外),即cell = [[[NSBundle mainBundle] loadNibNamed:@"ZoomCustomVideoCell" owner:self options:nil] objectAtIndex:0];

I have no idea why it shows a memory leak there and what is solution for same 我不知道为什么它在那里显示内存泄漏,什么是相同的解决方案

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"ZoomCustomVideoCell";

    ZoomCustomVideoCell *cell = (ZoomCustomVideoCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ZoomCustomVideoCell" owner:self options:nil] objectAtIndex:0];

        cell.m_zoomMsg = [[[ZoomMessage alloc] init] autorelease];
        [[cell m_zoomMsg] initWithJSON:[m_tmpVideoList objectAtIndex: indexPath.row]];
        [[cell videoLabel] setText:[cell.m_zoomMsg _from]];
        [[cell m_labelLocation] setText:[NSString stringWithFormat:@"%@", cell.m_zoomMsg._location]];
        [[cell videoLabelB] setText:[cell.m_zoomMsg _uploadDesc]];
        NSLog(@"UserName: %@", [[cell videoLabel] text]);

        [cell refreshImage];

    }

    return cell;
}

The loadNibNamed:owner:options: method takes the argument passed as owner: as the “File's Owner” of the nib. loadNibNamed:owner:options:方法采用作为owner:传递的参数作为笔尖的“文件所有者”。 This means that outlets in the nib will be connected to whatever you pass as the owner. 这意味着笔尖中的插座将连接到您作为所有者通过的所有对象。 Since you are passing self as the owner, it will be overwriting any previously assigned outlets for self with ones from the newly loaded nib. 由于您将self作为所有者传递,因此它将用新加载的笔尖中的插座覆盖以前为self分配的所有插座。 To establish outlet connections, the nib loader uses setValue:forKey: on the provided owner. 为了建立出口连接,笔尖加载器在提供的所有者上使用setValue:forKey: If you have set up your outlets as properties with the correct memory management, then there should be no leak. 如果您已将出口设置为具有正确内存管理的属性,则应该没有泄漏。 If you only have your outlets as instance variables then (it is unclear but I am assuming) that the objects are retained automatically as they are set. 如果仅将出口作为实例变量,则(尚不清楚,但我假设)是,对象在设置时会自动保留。

There are two solutions here: 这里有两种解决方案:

  1. Provide proper memory management for your outlets, eg turn them into properties if they aren't already and ensure that they have the correct memory management attributes. 为您的插座提供适当的内存管理,例如,将它们变成属性(如果尚未使用),并确保它们具有正确的内存管理属性。

  2. Provide a different owner to the loadNibNamed:owner:options: method, one that doesn't already have any outlets established and one that you know will handle the outlets appropriately. loadNibNamed:owner:options:方法提供一个不同的所有者,一个尚未建立任何出口的loadNibNamed:owner:options: ,您知道的一个loadNibNamed:owner:options:将适当地处理这些出口。

This line causes the memory leak: 此行导致内存泄漏:

cell = [[[NSBundle mainBundle] loadNibNamed:@"ZoomCustomVideoCell" owner:self options:nil] objectAtIndex:0];

change it to: 更改为:

cell = [[[[NSBundle mainBundle] loadNibNamed:@"ZoomCustomVideoCell" owner:self options:nil] objectAtIndex:0] autorelease];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM