RestKit/RestKit — Issue #548: Leak with RKRequestBackgroundPolicyContinue
Author: sylvainlaurent
Created: 2012-02-06 13:54:03+00:00
State: open

When using RKRequestBackgroundPolicyContinue instead of the default RKRequestBackgroundPolicyNone, the RKObjectLoader (and therefore RKRequest) is never released, and leaks.

I think the problem lies in the block passed to app.beginBackgroundTaskWithExpirationHandler (see RKRequest:447) : it captures a reference to self and _delegate and retains them.
But the block is only released when calling app.endBackgroundTask , which is supposed to be called during dealloc but since we have a retain cycle, it is never called....

Here is a possible fix in RKrequest.m lines 444 and on, using local variable with __block to prevent the block from retaining self and _delegate :

// Fork a background task for continueing a long-running request
__block RKRequest* weakSelf = self;
__block id weakDelegate = _delegate;
_backgroundTaskIdentifier = [app beginBackgroundTaskWithExpirationHandler:^{
RKLogInfo(@"Background request time expired, canceling request.");

[weakSelf cancelAndInformDelegate:NO];
[weakSelf cleanupBackgroundTask];

if ([weakDelegate respondsToSelector:@selector(requestDidTimeout:)]) {
[weakDelegate requestDidTimeout:weakSelf];
}
}];