3

다음 메소드를 통해 API 요청에 대한 콜백으로 Block을 사용하려고합니다. 이 메서드는이 메서드의 블록에 중첩 된 Block을 사용합니다. 그것은 작동합니다 ... 그러나,콜백 블록에서 NSJSONSerialization 누수가 발생했습니다.

이 메서드를 호출 한 개체가 할당이 해제되면 NSJSONSerialization은 방대한 메모리 누수를 덤프합니다. 모든 것이 실행되는 동안 그것은 모두 좋은 것입니다. 누출은 요청한 객체가 사라진 후에 만 ​​발생합니다. 누수는 거의 모든 NSPlaceHolder 유형입니다.

내 재치가 끝나고 어떤 아이디어라도 높이 평가합니다.

- (void)sendRequestUsingNSURLConnectionWith:(NSURLRequest *)request andCallback:(void (^)(id))handler 
     { 
      __block __typeof__(self)blockSelf = self; 

      // CL: build a block to be run asynchronously 
      ApiClientCallback handleResponse = [[^(NSURLResponse *response, NSData *data, NSError *error) { 

       id results = nil; 

       // CL: http errors would be caught here. 
       if (error) { 
        NSLog(@"[%@ %@] HTTP error: %@", NSStringFromClass([blockSelf class]), NSStringFromSelector(_cmd), error.localizedDescription); 
        results = error; 
       } 
       else { 
       // CL: parse the JSON 
        NSError *jsonError = nil; 
        NSError *apiError = nil; 
        if (data) { 
         results = [NSJSONSerialization JSONObjectWithData:data 
                    options:NSJSONReadingMutableContainers 
                    error:&jsonError]; 
        } 
        else { 
         results = nil; 
        } 
        // CL: json errors would be caught here. 
        if (jsonError) { 
         NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([blockSelf class]), NSStringFromSelector(_cmd), error.localizedDescription); 
         results = error; 
        } 
        // CL: Check for API errors. 
        else if ([blockSelf checkApiErrorCode:results error:&apiError]) { 
         //CL: if there's an error make the NSError object the result. 
         if (apiError) results = apiError; 
        } 
       } 
       // CL: Send result to the completion block of the requesting object 
       handler(results); 

      } copy] autorelease]; 

      [NSURLConnection sendAsynchronousRequest:request 
               queue:self.opQueue 
            completionHandler:handleResponse]; 
     } 
+1

"NSJSONSerialization이 대량의 메모리 누수를 덤프합니다"라고 설명하십시오. – hooleyhoop

+0

문제점을 발견했으며 그 원인을 완전히 당황스럽게 생각합니다. 누수의 원인이 된 요청 객체는 다른 객체의 하위 클래스였습니다. 나는 실제로 서브 클래스에 [super dealloc]을 포함하는 것을 잊었다는 것을 깨달았다. 서브 클래스는 요청에 의해 채워지는 몇 개의 속성을 가지고 있는데, 슈퍼는 약 15 개가 있습니다. 테스트를 위해 나는 dealloc 전에 요청을 여러 번 실행 했으므로 큰 누출이있었습니다. 도! – GnarlyDog

+1

빌드 설정을 확인하지 않으려는 경우 [super dealloc]을 놓친 경우 경고 메시지가 표시됩니다. – hooleyhoop

답변

0

요청마다 여기에 결론을 문서화합니다. 하위 클래스의 dealloc 메소드에서 super dealloc을 호출하는 것을 잊어 버렸습니다. 이로 인해 할당 해제시 보유한 등록 정보가 모두 누설됩니다. 프로그래머 오류. 이 시나리오는 사전 ARC에서 발생했습니다.