views:

396

answers:

2

I'm implementing back-end data loading with a server by creating an NSOperation and opening NSURLConnections synchronously.

This code was working perfectly in both the simulator and the device until I added a progress bar with notifications going back to the main thread.

- (void)start {
     // stop execution if it's cancelled

     if([self isCancelled]) {
          [self willChangeValueForKey:@"isFinished"];
          finished = YES;
          [self didChangeValueForKey:@"isFinished"];
          return;
     } 

     // If the operation is not canceled, begin executing the task.
     [self willChangeValueForKey:@"isExecuting"];
     [NSThread detachNewThreadSelector:@selector(main ) toTarget:selfwithObject:nil];
     executing = YES;
     [self didChangeValueForKey:@"isExecuting"];
}

Now I'm getting EXC_BAD_ACCESS crashes in the synchronous call.

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/index.php", rootURL]];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[bodyString dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];

NSError *error = nil;
NSURLResponse *response = nil;
receivedData = [[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error] mutableCopy];

The stacktrace runs as follows:

#0  0x3430debc in objc_msgSend ()
#1  0x3136f996 in NSPopAutoreleasePool ()
#2  0x31374160 in -[NSAutoreleasePool release] ()
#3  0x331a0408 in _UIApplicationHandleEvent ()
#4  0x325339c4 in PurpleEventCallback ()
#5  0x3147a52a in CFRunLoopRunSpecific ()
#6  0x31479c1e in CFRunLoopRunInMode ()
#7  0x3314ec08 in -[UIApplication _run] ()
#8  0x3314d230 in UIApplicationMain ()
#9  0x00002ab0 in main (argc=1, argv=0x2ffff504)

With NSZombieEnabled, I get

*** -[NSDateComponents release]: message sent to deallocated instance 0x172fd0

I know that the connection is being established (looked at the server logs, and the request is going through). There is no NSDateComponent object being allocated anywhere in the NSOperation code prior to the NSURLConnection.

I've been trying to work out what I suddenly did wrong with NSURLConnection to have it hate me so.

Any help is greatly appreciated!

+1  A: 

While I dont know exactly what your problem is, I know that I used to get random errors and crashes when using NSURLConnection too. Then I found ASIHTTPRequest and have never looked back. It is a drop in replacement for NSURLConnection and runs off of CFNetwork. It is very stable (I use it in all my projects) and comes with a bunch of goodies like built in progress bar support and partial download-resuming. It even can write directly to disk to save on RAM, important in the iPhone.

coneybeare
Just tried using ASIHTTPRequest, and it's getting errors when trying to decompress my response data. Never happened with NSURLConnection.
serpah
A: 

I posted this question on the developer forums as well, and got the suggestion to remove the concurrency. I removed the offending line:

[NSThread detachNewThreadSelector:@selector(main ) toTarget:selfwithObject:nil];

I also stopped overriding start, and only overrode main.

NSURLConnection stopped crashing.

serpah