Skip to content

Commit

Permalink
Added new clearDelegatesAndCancel method
Browse files Browse the repository at this point in the history
Fix leak in iPad sample
  • Loading branch information
pokeb committed Nov 13, 2010
1 parent 4b6b9af commit 0d9fa92
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Classes/ASIHTTPRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,8 @@ typedef void (^ASIDataBlock)(NSData *data);
// Run request in the background
- (void)startAsynchronous;


// Clears all delegates and blocks, then cancels the request
- (void)clearDelegatesAndCancel;

#pragma mark HEAD request

Expand Down
42 changes: 40 additions & 2 deletions Classes/ASIHTTPRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#import "ASIDataCompressor.h"

// Automatically set on build
NSString *ASIHTTPRequestVersion = @"v1.7-137 2010-11-10";
NSString *ASIHTTPRequestVersion = @"v1.7-139 2010-11-13";

NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";

Expand Down Expand Up @@ -375,36 +375,58 @@ - (void)releaseBlocksOnMainThread
NSMutableArray *blocks = [NSMutableArray array];
if (completionBlock) {
[blocks addObject:completionBlock];
[completionBlock release];
completionBlock = nil;
}
if (failureBlock) {
[blocks addObject:failureBlock];
[failureBlock release];
failureBlock = nil;
}
if (startedBlock) {
[blocks addObject:startedBlock];
[startedBlock release];
startedBlock = nil;
}
if (headersReceivedBlock) {
[blocks addObject:headersReceivedBlock];
[headersReceivedBlock release];
headersReceivedBlock = nil;
}
if (bytesReceivedBlock) {
[blocks addObject:bytesReceivedBlock];
[bytesReceivedBlock release];
bytesReceivedBlock = nil;
}
if (bytesSentBlock) {
[blocks addObject:bytesSentBlock];
[bytesSentBlock release];
bytesSentBlock = nil;
}
if (downloadSizeIncrementedBlock) {
[blocks addObject:downloadSizeIncrementedBlock];
[downloadSizeIncrementedBlock release];
downloadSizeIncrementedBlock = nil;
}
if (uploadSizeIncrementedBlock) {
[blocks addObject:uploadSizeIncrementedBlock];
[uploadSizeIncrementedBlock release];
uploadSizeIncrementedBlock = nil;
}
if (dataReceivedBlock) {
[blocks addObject:dataReceivedBlock];
[dataReceivedBlock release];
dataReceivedBlock = nil;
}
if (proxyAuthenticationNeededBlock) {
[blocks addObject:proxyAuthenticationNeededBlock];
[proxyAuthenticationNeededBlock release];
proxyAuthenticationNeededBlock = nil;
}
if (authenticationNeededBlock) {
[blocks addObject:authenticationNeededBlock];
[authenticationNeededBlock release];
authenticationNeededBlock = nil;
}
[[self class] performSelectorOnMainThread:@selector(releaseBlocks:) withObject:blocks waitUntilDone:[NSThread isMainThread]];
}
Expand Down Expand Up @@ -648,6 +670,23 @@ - (void)cancel
[self performSelector:@selector(cancelOnRequestThread) onThread:[[self class] threadForRequest:self] withObject:nil waitUntilDone:NO];
}

- (void)clearDelegatesAndCancel
{
[[self cancelledLock] lock];

// Clear delegates
[self setDelegate:nil];
[self setQueue:nil];
[self setDownloadProgressDelegate:nil];
[self setUploadProgressDelegate:nil];

// Clear blocks
[self releaseBlocksOnMainThread];

[[self cancelledLock] unlock];
[self cancel];
}


- (BOOL)isCancelled
{
Expand Down Expand Up @@ -1891,7 +1930,6 @@ - (void)requestFinished
}
#if NS_BLOCKS_AVAILABLE
if(completionBlock){
__block ASIHTTPRequest *blockCopy = self;
completionBlock();
}
#endif
Expand Down
1 change: 1 addition & 0 deletions iPhone Sample/WebPageViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ - (void)dealloc
[webView release];
[responseField release];
[urlField release];
[requestsInProgress release];
[super dealloc];
}

Expand Down

0 comments on commit 0d9fa92

Please sign in to comment.