2013-08-21 55 views
0

我正在使用AFNetworking發送請求,但是當請求完成其作業時,我得到了儀器中的這些內存分配,並且它們永遠留在那裏...如果我嘗試啓動另一個請求,應用程序將收到內存多次警告,因此被迫關閉。使用AFNetworking請求分配

Graph Category Live Bytes # Living # Transitory Overall Bytes # Overall # Allocations (Net/Overall) 
1 CFString (store) 4,86 MB 29 3595 30,78 MB 3624 <XRRatioObject: 0x7ffd1c5dc3f0> %0.00, %0.17 
1 Malloc 3,91 MB 3,91 MB 1 1 7,83 MB 2 <XRRatioObject: 0x7ffd1c5dc3f0> %0.00, %0.00 

而對於每一個內分配:

首先

# Address Category Timestamp Live Size Responsible Library Responsible Caller 
0 0x4d28000 CFString (store) 01:12.165.450 • 4923392 CFNetwork _ZL19_cacheKeyForRequestPK13_CFURLRequest 
1 0x36b8000 CFString (store) 00:54.705.461 • 172032 CFNetwork _ZL19_cacheKeyForRequestPK13_CFURLRequest 
2 0x1e3f5a00 CFString (store) 01:12.326.108 • 1024 CFNetwork _ZL19_cacheKeyForRequestPK13_CFURLRequest 

# Address Category Timestamp Live Size Responsible Library Responsible Caller 
0 0x6903000 Malloc 3,91 MB 01:01.583.198 • 4104192 Foundation -[NSConcreteMutableData initWithLength:] 

編輯1

這是我們使用的是創建的請求的代碼:

// Do a json request 
+ (void) doJsonRequestWithPath 
: (NSString *) path 
withMethod: (NSString *) method 
withParameters: (id) parameters 
withHeaders: (NSDictionary *) headers 
onSuccess: (void(^)(NSURLRequest *, NSHTTPURLResponse *, id))success 
onFailure: (void(^)(NSURLRequest *, NSHTTPURLResponse *, NSError *, id))failure 
{  
    // Register json request operation 
    [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; 
    // Json parameter encoding 
    [httpClient setParameterEncoding:AFJSONParameterEncoding]; 

    NSMutableURLRequest *request; 


    if ([parameters isKindOfClass:[NSDictionary class]]) { 
     // Create a request with the http client + path and params 
     request = [httpClient requestWithMethod:method path:path parameters:parameters]; 
    } 
    else { 
     request = [httpClient requestWithMethod:method path:path parameters:nil]; 
     [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
     //[request setValue:@"100-continue" forHTTPHeaderField:@"Expect"]; 
     [request setHTTPBody:[parameters dataUsingEncoding:NSUTF8StringEncoding]]; 
    } 

    [request setCachePolicy:NSURLRequestReloadIgnoringCacheData]; 
    [request setTimeoutInterval:20]; 

    // Add headers to the request if there is any 
    if (headers != nil) 
     [self addHeadersToRequest:request fromDictionary:headers]; 

    // Create a json request operation 
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:success failure:failure]; 



    // Starts the operation 
    [operation start]; 
} 

編輯2

我們有一個遞歸調用

- (void)sendFingerprintAtIndex:(NSInteger) index withGuid:(NSString *) guid 
{ 
    SendActivationRequest *sendActivationRequest = [[SendActivationRequest alloc] 
                initWithGuid: guid 
                andWithTotalImages:4 
                andWithImageIndex:index 
                andImageType:2 //digital 
                andWithImage:image]; 

[self.activationDao sendActivationRequest:sendActivationRequest withCompletionBlock:^(NSString *hash, NSArray *errors) {   
     if (hash) { 

       . 
       . 
       . 
      } 
      else { 
       // recursive call 
       [self sendFingerprintAtIndex:newIndex withGuid:guid]; 
      } 
     } 
     else 
     { 
      self.completionBlockController(NO, [errors objectAtIndex:0]); 
     } 
    }]; 
} 

而在DAO

- (void) sendActivationRequest:(SendActivationRequest *) sendActivationRequest withCompletionBlock:(void (^)(NSString *, NSArray *))completionBlock 
{ 
    // Path 
    NSString *path = @"EnviarAtivacao"; 

    id params = [sendActivationRequest getParams]; 
    NSDictionary *headers = [sendActivationRequest getHeader]; 

     // Do a json post request 
    [self doJsonPostRequestWithPath:path 
        withParameters:params 
         withHeaders:headers 
          onSuccess:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) { 
           . 
           . 
           .     

          } onFailure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *err, id json) { 
           . 
           . 
           . 


          }]; 

} 

謝謝!

+0

您能否提供一些示例代碼來展示您如何使用AFNetworking? – Jeff

+0

我猜你在成功或失敗區域有一個保留週期。基於'XRRatioObject'對象的分配,我認爲它與Sparrow框架有關。你能發佈你如何調用這個方法嗎? –

+0

也許遞歸調用是亞倫的問題。你怎麼看? –

回答

0

這些都是從應用程序的共享NSURLCache實例分配。

如果我嘗試啓動另一個請求,應用程序會多次收到內存警告,因此它會被強制關閉。

NSURLCache應該在發出內存警告時自動丟棄它的內存中緩存。我不確定「它被迫關閉」是什麼意思,但這可能是您在視圖控制器中釋放未使用的內存時發生內存警告的問題。

+0

感謝您的幫助,mattt,我通過禁用緩存解決了問題,是好的,否則我會遇到問題? –

0

您在完成塊中保留了循環。你不應該在任何塊中引用「自我」。試着做這樣的事情

__weak MyClass *wSelf = self; 
MyCompletionBlock completionBlock = ^{ 
    __strong MyClass *sSelf = wSelf; 
    [sSelf doThings]; 
} 
+0

沒有工作,爲所有塊更改它,但分配保持增長到幾乎100 MB。 –

+0

檢查你的遞歸​​調用,並確保你有一個終止條件,並且你不在無限遞歸循環中。放入一條NSLog語句,看看它被調用了多少次。 – JonahGabriel

+0

遞歸調用正常工作。 –