2011-02-08 36 views
0

我不明白! 儀器正顯示出我泄漏這個方法泄漏在哪裏?

-(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)。特別是在循環中!

但有時甚至儀器崩潰!

回答

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


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

這種模式是致命的;你直接釋放一個實例變量,可能留下一個懸掛指針,然後通過set方法(通過點語法)分配一個值。

set方法將首先嚐試釋放locationData。

它不會崩潰的唯一原因 - 就像喬所指出的那樣 - 就是您首先將locationData過度保留。

在-dealloc之外,使用self.locationData = nil;來釋放和刪除實例變量。

3

如果屬性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]; 
} 
+0

**謝謝我會立即嘗試** – 2011-02-08 16:52:33

+0

如果您在進行更改後遇到`EXC_BAD_ACCESS`,則爲您添加了一些額外信息。 – Joe 2011-02-08 17:01:19