Thursday, 22 August 2013

NSURLConnection crash on simultaneous failing connections IOS6.1 ARC Simulator

NSURLConnection crash on simultaneous failing connections IOS6.1 ARC
Simulator

I am new with IOS and I am still not sure if I've hit a bug, or I am doing
something wrong. Although given the strange behaviour I get, I tend to
think this is a bug in how IOS tries to reuse connections. I wonder if
anyone knows about this and if there are workarounds.
Happens on IOS 6.1 ARC enabled, simulator. Even more strange is I can't
get to crash it on the phone, so now I am wondering should I be afraid of
this or not.
I am running multiple simultaneous http connections, once a connection
finish or fails I am starting a new one. Until now I have 3 situations:
Case 1: the server exists and works, each request is successful, works.
Case 2: the server exists but won't listen on the given port, works.
Case 3: the server is offline (eg. the machine is down), will crash after
100-2000 requests.
Edit: modified the code according NicholasHart suggestion, the problem
persists
#import "ViewController.h"
@interface ViewController ()
{
NSURLRequest *request;
}
@end
@implementation ViewController
- (IBAction)test:(id)sender {
request = [NSURLRequest requestWithURL:[NSURL
URLWithString:@"http://192.168.1.178:8888/r.php"]
cachePolicy:NSURLCacheStorageNotAllowed
timeoutInterval:10.0];
NSLog(@"starting %@", [NSThread currentThread]);
NSURLConnection *conn;
for(int i=0;i<6;i++) conn = [[NSURLConnection alloc]
initWithRequest:request delegate:self];
}
#pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response {}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData
*)data {}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {return nil;}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//[connection cancel]; //not needed, suggested by NicholasHart
NSLog(@"ok %@", [NSThread currentThread]);
NSURLConnection *conn = [[NSURLConnection alloc]
initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError
*)error{
//[connection cancel]; //not needed, suggested by NicholasHart
NSLog(@"error %@", [NSThread currentThread]);
NSURLConnection *conn = [[NSURLConnection alloc]
initWithRequest:request delegate:self];
}
@end
For case 3 I have different crashes similar to:

libdispatch.dylib`_dispatch_retain:
0x49f98f0: movl 4(%esp), %eax
0x49f98f4: cmpl $2147483647, 4(%eax)
0x49f98fb: je 0x49f9910 ; _dispatch_retain + 32
0x49f98fd: addl $4, %eax
0x49f9900: movl $1, %ecx
0x49f9905: lock
0x49f9906: xaddl %ecx, (%eax)
0x49f9909: incl %ecx
0x49f990a: testl %ecx, %ecx
0x49f990c: jg 0x49f9910 ; _dispatch_retain + 32
0x49f990e: ud2
0x49f9910: ret

No comments:

Post a Comment