哪里漏了?

发布于 2024-10-16 18:00:44 字数 3728 浏览 5 评论 0原文

我不明白! 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)。特别是在循环中!

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

I don't get it!
Instruments is showing me a leak in this method

-(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];

}

Is it possible that [nsdictionaryobject objectForKey:@"xy"]; is causing a leak?

Because in instrument especially these lines are coloured. As you can see I was releasing everything.
I'm quite desperate with that app. I even began to replace all the convenient constructors through alloc/init/release (e.g. initWithFormat instead of stringWithFormat). Especially in loops!

But sometimes even instruments crash!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞取 2024-10-23 18:00:44
if (locationData != nil) {
        [locationData release];
    }


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

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

set方法会首先尝试释放locationData。

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

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

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


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

This pattern is deadly; you are directly releasing an instance variable, potentially leaving behind a dangling pointer, then assigning a value through the set method (via the dot syntax).

The set method will first try to release locationData.

The only reason why it doesn't crash -- as Joe indicated -- is that you are over-retaining locationData in the first place.

Outside of -dealloc, use self.locationData = nil; to both release and nil out the instance variable.

合久必婚 2024-10-23 18:00:44

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

//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];
}

If the property locationData is set to retain you are creating a memory leak on the following line

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

Edit:

That may introduce a new problem for you with the following lines

//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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文