objective-c - 回调 block 中的 NSJSONSerialization 泄漏

标签 objective-c memory-management nsurlconnection objective-c-blocks nsjsonserialization

我正在尝试通过以下方法使用 Blocks 作为 API 请求的回调。此方法采用嵌套在此方法的 block 中的 block 。它有效……但是,

如果调用此方法的对象被释放,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];
        }

最佳答案

根据要求,我在这里记录了结论。原来是我忘记在子类的dealloc方法中调用super dealloc了。这导致 super 在解除分配时泄漏其所有保留的属性。程序员错误。请注意,这种情况发生在 ARC 之前。

关于objective-c - 回调 block 中的 NSJSONSerialization 泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8774399/

相关文章:

iphone - 需要将NSData转换为NSArray

iphone - objective-C ERROR_CODE_TIMEOUT 值

objective-c - 以编程方式发送 PayPal 付款 Objective C

ios - 在 NSString 中查找俄语字符

objective-c - 如何使用 MKAnnotation

objective-c - 合并 NSDictionary

c++ - BGL : How do I store edge_descriptors and vertex_descriptors efficiently?

.net - .NET 中的托管资源与非托管资源。有什么不同?

objective-c - 添加到 NSScrollView 的 documentView 的 subview 未显示

PHP 使用的内存多于所需的文件