Odd EXEC_BAD_ACCESS after executing URLRequest
Odd EXEC_BAD_ACCESS after executing URLRequest
- Subject: Odd EXEC_BAD_ACCESS after executing URLRequest
- From: marc hoffman <email@hidden>
- Date: Tue, 30 Dec 2008 17:10:07 +0100
Hi,
i'm hoping someone has an idea here - i'm seeing odd crashes after
doing URL requests. the first request completes fine (up to it
triggering connectionDidFinishLoading:), but shortly after or while
doing a subsequent request, i get a EXC_BAD_ACCESS in what seems to be
a secondary worker thread created by the connection (callstack is
below). this only happens on the iPhone device (2.2) - in desktop Mac
OS X apps or in the iPhone Simulator, the same code seems to run fine.
i've been going over this code again and again and can't find anything
obvious wrong. as far as i can tell there's nothing (besides
releasing) that i need to do to clean up after a connection is done,
right? if so, why is there obvious processing goping on with the
connection *after* it finished?
my code basically looks like this:
- (NSData *)sendMessage:(NSData *)requestData contentType:(NSString
*)aContentType
{
// these are class fields
responseData = nil;
responseLength = 0;
error = nil;
done = NO;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:timeout];
@try
{
[request setHTTPMethod:@"POST"];
[request setHTTPBody:requestData];
[request setValue:[NSString stringWithFormat:@"%d",
[requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:aContentType forHTTPHeaderField:@"Content-
Type"];
NSURLConnection *conn = [[NSURLConnection
connectionWithRequest:request delegate:self] retain];
@try
{
[conn start];
while (!done)
{
[[NSRunLoop currentRunLoop]
runMode:NSDefaultRunLoopMode beforeDate:[NSDate
dateWithTimeIntervalSinceNow:timeout]];
}
if (error)
@throw [NSException exceptionWithName:@"RO Exception"
reason:[NSString
stringWithFormat:@"Error dispatching message: %@", [error
localizedDescription]]
userInfo:nil];
}
@finally
{
[conn release]; // removed for testing - makes no diff
either way
}
}
@finally
{
[request release]; // removed for testing - makes no diff
either way
}
// don't hold on to responseData ourselves
NSData *temp = [responseData autorelease]; // autorelease
removed for testing - too makes no diff either way
responseData = nil;
return temp;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:
(NSURLResponse *)aResponse
{
responseLength = [aResponse expectedContentLength];
NSLog(@"connection:didReceiveResponse:, expecting %d bytes",
responseLength);
if (responseLength = -1) responseLength = 0;
responseData = [[NSMutableData alloc]
initWithCapacity:responseLength];
// [...]
}
- (void)connection:(NSURLConnection *)connection didReceiveData:
(NSData *)data
{
NSLog(@"connection:didReceiveData:%d",[data length]);
[responseData appendData:data]; // i'm assuming this copies the
bytes and keeps no ref to "data"
// [...]
}
- (void)connection:(NSURLConnection *)connection didFailWithError:
(NSError *)aError
{
NSLog(@"connection:didFailWithError:");
done = YES;
error = [aError retain];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"connectionDidFinishLoading:");
done = YES;
}
Call stack for the EXEC_BAD_ACCESS:
#0 0x300c8c18 in objc_msgSend
#1 0x3023a4a4 in CFRetain
#2 0x3120ae62 in httpReadStreamCB
#3 0x30293806 in _CFStreamSignalEventSynch
#4 0x30269a8a in CFRunLoopRunSpecific
#5 0x30269326 in CFRunLoopRunInMode
#6 0x306a4c24 in +[NSURLConnection(NSURLConnectionReallyInternal)
_resourceLoadLoop:]
#7 0x30672e08 in -[NSThread main]
#8 0x30672cd6 in __NSThread__main__
#9 0x3145d78c in _pthread_body
any ideas would be appreciated.
thanx,
marc
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden