Skip to content

Instantly share code, notes, and snippets.

@0xced
Created July 6, 2011 15:09
Show Gist options
  • Save 0xced/1067463 to your computer and use it in GitHub Desktop.
Save 0xced/1067463 to your computer and use it in GitHub Desktop.
Demonstrates NSURL crash when subclassing, see http://www.openradar.me/9729706
#import <Foundation/Foundation.h>
@interface MyURL : NSURL
@end
@implementation MyURL
@end
void testURL(Class urlClass, NSString *string)
{
NSURL *url = [[[urlClass alloc] initWithString:string relativeToURL:[NSURL URLWithString:@"http://www.apple.com"]] autorelease];
NSString *absoluteString = [url absoluteString];
NSLog(@"[%@] %@", urlClass, absoluteString);
}
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
testURL([NSURL class], @"iphone");
testURL([NSURL class], @"");
testURL([MyURL class], @"iphone");
testURL([MyURL class], @"");
[pool release];
return 0;
}
@aglee
Copy link

aglee commented May 28, 2012

Looks like the mere use of a subclass triggers the bug. The subclass doesn't need any methods in it. You could trim a few lines, keeping the same main() function:

@interface MyURL : NSURL
@end

@implementation MyURL
@end

void testInitURL(Class urlClass, NSString *string)
{
    NSURL *url = [[[urlClass alloc] initWithString:string relativeToURL:[NSURL URLWithString:@"http://www.apple.com"]] autorelease];
    
    NSString *absoluteString = [url absoluteString];
    NSLog(@"[%@] %@", urlClass, absoluteString);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment