The problem is this test code crashes pathURLHandle is sent loadInForeground. I posted this macosx-dev@omingroup.com and although no one had an answer for the cause, it was suggested I work around it with NSURLConnection in 10.2.7 and up. Nevertheless I don't see why it crashes.

Here's the Archived Xcode test project (36KB)

The test program is after the mail.

Here's the mail I sent out:

I have some code that displays images by getting the data from websites. It's crashing in NSURLHandle's resourceData method when the url comes back with bogus data. I don't know how to defend against that. Basically the simplified relevant code is:

NSURL *pathURL;
NSURLHandle *pathURLHandle;
NSData *searchData;
NSString *urlString;

pathURL = [ NSURL URLWithString:@"http://apple.com/foo/"];
if (pathURL == nil) {
return;
}
pathURLHandle = [ pathURL URLHandleUsingCache:YES];
searchData = [ pathURLHandle resourceData];
NSImage *image = [[[ NSImage alloc] initWithData:searchData] autorelease];
...


It crashes in "searchData = [ pathURLHandle resourceData];".

Is there something I can test before I call - resourceData to ensure it won't segv?

thanks

#0 0x908311f4 in objc_msgSend
#1 0x9274b8a4 in _CFNetConnectionResponseIsComplete
#2 0x9274b864 in CFHTTPMessageIsHeaderComplete
#3 0x9274264c in CFHostScheduleWithRunLoop
#4 0x901c395c in CFReadStreamRead
#5 0x90afc96c in -[NSHTTPURLHandle loadInForeground]
#6 0x90afe54c in -[NSURLHandle resourceData]
#7 0x002e3d78 in -[MyObject myAction:] at MyObject.m:23


#import "MyObject.h"

@implementation MyObject

- (IBAction)myAction:(id)sender
{
	NSURLHandle *pathURLHandle;
	NSData *searchData;
	NSString *urlString;
	NSArray *statusStrings = [NSArray arrayWithObjects:@"Not Loaded", @"Succeeded", @"In Progress", @"Failed", nil];
	NSURLHandleStatus status;

	if ( [ myOutlet1 state] == NSOnState) {
		urlString = [ myOutlet1 title];
	} else {
		urlString = [ myOutlet2 title];
	}
    
	NSURL *pathURL = [ NSURL URLWithString:urlString];
	if (pathURL == nil) {
		NSBeep();
		return;
	}
	pathURLHandle = [ pathURL URLHandleUsingCache:YES];
	[pathURLHandle loadInForeground];	
	status = [ pathURLHandle status];
	
	if (status == NSURLHandleLoadSucceeded) {
		searchData = [ pathURLHandle resourceData];
		NSImage *image = [[[ NSImage alloc] initWithData:searchData] autorelease];
		if ([ image isValid]) {
			[myOutlet setImage:image];
		} else {
			NSBeep();
		}
	} else {
		NSBeep();
		NSLog(@"status1: %@", [ statusStrings objectAtIndex:status]);
	}
}

@end



Wed, Aug 11, 2004