泄漏点在哪里?

Posted

技术标签:

【中文标题】泄漏点在哪里?【英文标题】:Where is the leak? 【发布时间】:2011-02-08 16:45:20 【问题描述】:

我不明白! Instruments 显示此方法存在泄漏

-(void)loadData

    if (locationData != nil) 
        [locationData release];
    

self.locationData = [[NSMutableArray alloc] init];

NSData *recievedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://htmlwin001.******.net/blutalkasp/locations2.asp?uid=%@&von=%d&bis=%d", [[UIDevice currentDevice] uniqueIdentifier], von, bis]]];

NSString *recievedString = [[NSString alloc] initWithData:recievedData encoding:NSUTF8StringEncoding];

SBJsonParser *json = [[SBJsonParser alloc] init];
NSMutableDictionary *jsonData = [json objectWithString : recievedString];

NSString *tmpLocationData;
for (NSDictionary *location in [jsonData objectForKey:@"items"]) 
    Location *newLocation = [[Location alloc] init];
    tmpLocationData = [[NSString alloc]initWithFormat:@"%@", [location objectForKey:@"id"]];
    [newLocation setLocationID:tmpLocationData];
    [tmpLocationData release];
    tmpLocationData = [[NSString alloc]initWithFormat:@"%@", [[location objectForKey:@"locationname"] gtm_stringByUnescapingFromHTML]];
    [newLocation setLocationName:tmpLocationData];
    [tmpLocationData release];
    tmpLocationData = [[NSString alloc]initWithFormat:@"%@",[location objectForKey:@"locationdistance"]];
    [newLocation setLocationDistance:tmpLocationData];
    [tmpLocationData release];
    tmpLocationData = [[NSString alloc]initWithFormat:@"%@", [[location objectForKey:@"locationaddress"] gtm_stringByUnescapingFromHTML]];
    [newLocation setLocationAdress:tmpLocationData];
    [tmpLocationData release];
    tmpLocationData = [[NSString alloc]initWithFormat:@"%@", [[location objectForKey:@"locationdescription"] gtm_stringByUnescapingFromHTML]];
    [newLocation setLocationDescription:tmpLocationData];
    [tmpLocationData release];

    NSNumber *tmpLocationLat = [[NSNumber alloc] initWithInteger:[[location objectForKey:@"locationlatitude"]integerValue]];
    [newLocation setLocationPositionLat:tmpLocationLat];
    [tmpLocationLat release];

    NSNumber *tmpLocationLng = [[NSNumber alloc] initWithInteger:[[location objectForKey:@"locationlongitude"]integerValue]];
    [newLocation setLocationPositionLng:tmpLocationLng];
    [tmpLocationLng release];

    NSString *URL;
    URL = [location objectForKey:@"locationimage1"];
    URL = [URL stringByReplacingOccurrencesOfString:@"[SLASH]" withString:@"/"];
    NSString *tmpUrl1 = [[NSString alloc]initWithFormat:@"http://htmlwin001.******.net/blutalkasp/locationimages/data/%@", URL];
    [newLocation setLocationImageURL1:tmpUrl1];
    [tmpUrl1 release];

    URL = [location objectForKey:@"locationimage2"];
    URL = [URL stringByReplacingOccurrencesOfString:@"[SLASH]" withString:@"/"];
    NSString *tmpUrl2 = [[NSString alloc]initWithFormat:@"http://htmlwin001.******.net/blutalkasp/locationimages/data/%@", URL];
    [newLocation setLocationImageURL2:tmpUrl2];
    [tmpUrl2 release];

    URL = [location objectForKey:@"locationimage3"];
    URL = [URL stringByReplacingOccurrencesOfString:@"[SLASH]" withString:@"/"];
    NSString *tmpUrl3 = [[NSString alloc]initWithFormat:@"http://htmlwin001.******.net/blutalkasp/locationimages/data/%@", URL];
    [newLocation setLocationImageURL3:tmpUrl3]; //Leak geschlossen
    [tmpUrl3 release];

    [self.locationData addObject:newLocation];

    [newLocation release];
   
[recievedString release];
[json release];


[nsdictionaryobject objectForKey:@"xy"]; 是否可能导致泄漏?

因为在仪器中,尤其是这些线条是彩色的。正如你所看到的,我正在释放一切。 我对那个应用程序很绝望。我什至开始通过 alloc/init/release 替换所有方便的构造函数(例如 initWithFormat 而不是 stringWithFormat)。尤其是在循环中!

但有时甚至仪器也会崩溃!

【问题讨论】:

【参考方案1】:
if (locationData != nil) 
        [locationData release];
    


self.locationData = [[NSMutableArray alloc] init];

这种模式是致命的;您正在直接释放一个实例变量,可能会留下一个悬空指针,然后通过 set 方法(通过点语法)分配一个值。

set方法会先尝试释放locationData。

它没有崩溃的唯一原因——正如 Joe 指出的那样——是你首先过度保留了 locationData。

在 -dealloc 之外,使用 self.locationData = nil; 释放和清除实例变量。

【讨论】:

我会尝试你的建议。但不要着急,因为这个项目的客户遇到了麻烦。但我继续编写这段代码只是为了更多地了解objective-c。我想我需要一本关于这些东西的好书。【参考方案2】:

如果属性locationData 设置为retain,您将在以下行创建内存泄漏

//This is what is probably leaking
self.locationData = [[NSMutableArray alloc] init];
//Change that to
self.locationData = [[[NSMutableArray alloc] init] autorelease];

编辑:

以下几行可能会给您带来一个新问题

//Remove this check to release locationData because the property will properly 
//handle memory management for you just by setting it
if (locationData != nil) 
    [locationData release];

【讨论】:

谢谢,我马上试试 为您添加了一些额外信息,以防您在进行更改后遇到EXC_BAD_ACCESS 是的,这就是我注释掉这些行的原因。我有这种 MutableArrays 的其他方法。我正在以与删除此相同的方式更改它们 'if (arrData != nil) [arrData release]; ' 只要确保你在 dealloc 中释放 locationData 就可以了。 哦,是的,我忘了告诉'locationData' 是这个viewController 类的一个属性。它在 dealloc-method 中释放。

以上是关于泄漏点在哪里?的主要内容,如果未能解决你的问题,请参考以下文章

使用 nvm 时节点在哪里?

安装了 Spyder。点在哪里?

nextjs 应用程序的入口点在哪里?

nub点在哪里,nub看男女准吗

区块链游戏的爆点在哪里?

这里的内存泄漏在哪里?