2012-07-02 144 views
0

我有一些循環。在這個週期中,我提出了一些要求並獲得了回覆文本。在循環結束時,我出於某種原因休眠幾秒鐘並繼續迭代。 [VK朋友]中有大約500個對象,所以重複約500次,但是當它完成我的程序時,使用的內存比啓動時多得多。我使用ARC,我不明白爲什麼循環中分配的內存不會在每次迭代中釋放?這是正常的,還是我錯了?這段代碼爲什麼會泄漏內存?

for (Friend *friend in [Vk friends]) { 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"log" object:[NSString stringWithFormat:@"Visit %i/%i friend (earn %i coins)", ++count, [Vk friends].count, [UserState coins] - coinsBefore]]; 
    if (friend.helpPoints <= 0) continue; 
    strData = [NSString stringWithFormat:@"someparams=somevalues&param1=%@", [Vk authKey]]; 
    data = [strData dataUsingEncoding:NSUTF8StringEncoding]; 
    request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://someaddress/somepath?somegetparams=%@", [Vk userId]]]]; 
    request.HTTPMethod = @"POST"; 
    [request setValue:[NSString stringWithFormat:@"%i", [data length]] forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.43 Safari/536.11" forHTTPHeaderField:@"User-Agent"]; 
    [request setValue:@"http://blahblah.com" forHTTPHeaderField:@"Origin"]; 
    [request setValue:@"http://blahblah.com" forHTTPHeaderField:@"Referer"]; 
    [request setValue:@"ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4" forHTTPHeaderField:@"Accept-Language"]; 
    [request setValue:@"windows-1251,utf-8;q=0.7,*;q=0.3" forHTTPHeaderField:@"Accept-Charset"]; 
    [request setHTTPBody:data]; 
    data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 
    doc = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
    [NSThread sleepForTimeInterval:(helpTimeout + randDouble(min, max)) * 5.0]; 
} 
+0

所有變量都在上面聲明... –

回答

5

完全正常。如果你覺得你累積了太多的內存,將for循環的內部包裝到一個@autoreleasepool中,以便在每次循環迭代結束時回收內存。