Ben Copsey

Release blocks on main thread (hopefully closes gh-91)

@@ -24,7 +24,7 @@ @@ -24,7 +24,7 @@
24 #import "ASIDataCompressor.h" 24 #import "ASIDataCompressor.h"
25 25
26 // Automatically set on build 26 // Automatically set on build
27 -NSString *ASIHTTPRequestVersion = @"v1.7-131 2010-11-07"; 27 +NSString *ASIHTTPRequestVersion = @"v1.7-132 2010-11-10";
28 28
29 NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain"; 29 NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";
30 30
@@ -178,6 +178,7 @@ static NSOperationQueue *sharedQueue = nil; @@ -178,6 +178,7 @@ static NSOperationQueue *sharedQueue = nil;
178 178
179 #if NS_BLOCKS_AVAILABLE 179 #if NS_BLOCKS_AVAILABLE
180 - (void)performBlockOnMainThread:(ASIBasicBlock)block; 180 - (void)performBlockOnMainThread:(ASIBasicBlock)block;
  181 +- (void)releaseBlocksOnMainThread;
181 #endif 182 #endif
182 183
183 @property (assign) BOOL complete; 184 @property (assign) BOOL complete;
@@ -362,21 +363,59 @@ static NSOperationQueue *sharedQueue = nil; @@ -362,21 +363,59 @@ static NSOperationQueue *sharedQueue = nil;
362 [dataDecompressor release]; 363 [dataDecompressor release];
363 364
364 #if NS_BLOCKS_AVAILABLE 365 #if NS_BLOCKS_AVAILABLE
365 - [completionBlock release]; 366 + [self releaseBlocksOnMainThread];
366 - [failureBlock release];  
367 - [startedBlock release];  
368 - [headersReceivedBlock release];  
369 - [bytesReceivedBlock release];  
370 - [bytesSentBlock release];  
371 - [downloadSizeIncrementedBlock release];  
372 - [uploadSizeIncrementedBlock release];  
373 - [dataReceivedBlock release];  
374 - [proxyAuthenticationNeededBlock release];  
375 - [authenticationNeededBlock release];  
376 #endif 367 #endif
  368 +
377 [super dealloc]; 369 [super dealloc];
378 } 370 }
379 371
  372 +#if NS_BLOCKS_AVAILABLE
  373 +- (void)releaseBlocksOnMainThread
  374 +{
  375 + NSMutableArray *blocks = [NSMutableArray array];
  376 + if (completionBlock) {
  377 + [blocks addObject:completionBlock];
  378 + }
  379 + if (failureBlock) {
  380 + [blocks addObject:failureBlock];
  381 + }
  382 + if (startedBlock) {
  383 + [blocks addObject:startedBlock];
  384 + }
  385 + if (headersReceivedBlock) {
  386 + [blocks addObject:headersReceivedBlock];
  387 + }
  388 + if (bytesReceivedBlock) {
  389 + [blocks addObject:bytesReceivedBlock];
  390 + }
  391 + if (bytesSentBlock) {
  392 + [blocks addObject:bytesSentBlock];
  393 + }
  394 + if (downloadSizeIncrementedBlock) {
  395 + [blocks addObject:downloadSizeIncrementedBlock];
  396 + }
  397 + if (uploadSizeIncrementedBlock) {
  398 + [blocks addObject:uploadSizeIncrementedBlock];
  399 + }
  400 + if (dataReceivedBlock) {
  401 + [blocks addObject:dataReceivedBlock];
  402 + }
  403 + if (proxyAuthenticationNeededBlock) {
  404 + [blocks addObject:proxyAuthenticationNeededBlock];
  405 + }
  406 + if (authenticationNeededBlock) {
  407 + [blocks addObject:authenticationNeededBlock];
  408 + }
  409 + [[self class] performSelectorOnMainThread:@selector(releaseBlocks:) withObject:blocks waitUntilDone:[NSThread isMainThread]];
  410 +}
  411 +// Always called on main thread
  412 ++ (void)releaseBlocks:(NSArray *)blocks
  413 +{
  414 + // Blocks will be released when this method exits
  415 +}
  416 +#endif
  417 +
  418 +
380 #pragma mark setup request 419 #pragma mark setup request
381 420
382 - (void)addRequestHeader:(NSString *)header value:(NSString *)value 421 - (void)addRequestHeader:(NSString *)header value:(NSString *)value
@@ -17,6 +17,18 @@ @@ -17,6 +17,18 @@
17 - (BOOL)shouldRunOnMainThread { return YES; } 17 - (BOOL)shouldRunOnMainThread { return YES; }
18 18
19 #if NS_BLOCKS_AVAILABLE 19 #if NS_BLOCKS_AVAILABLE
  20 +#if TARGET_OS_IPHONE
  21 +// It isn't safe to allow the view to deallocate on a thread other than the main thread / web thread, so this test is designed to cause a crash semi-reliably
  22 +- (void)testBlockMainThreadSafety
  23 +{
  24 + NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
  25 + UIWebView *webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0,0,200,200)] autorelease];
  26 + ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
  27 + [request setCompletionBlock:^(ASIHTTPRequest *request) {[webView loadHTMLString:[request responseString] baseURL:url]; }];
  28 + [request startAsynchronous];
  29 +}
  30 +#endif
  31 +
20 - (void)testBlocks 32 - (void)testBlocks
21 { 33 {
22 NSData *dataToSend = [@"This is my post body" dataUsingEncoding:NSUTF8StringEncoding]; 34 NSData *dataToSend = [@"This is my post body" dataUsingEncoding:NSUTF8StringEncoding];
This diff was suppressed by a .gitattributes entry.