0

我是objective-c的新手,我试图编写一个小示例应用程序,从远程服务器获取一些XML并将其输出到控制台,但是当我这样做时,我得到一个我不明白的EXC_BAD_ACCESS:

    NSString *FeedURL = @"MYURLGOESHERE";
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:FeedURL]];
    NSURLResponse *resp = nil;
    NSError *err = nil;
    NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];
    NSString *theString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 

    NSLog(@"Response: %@", theString);];

    [resp release];
    [err release];

当我注释掉该[resp release]行时,我不再明白了,有人可以向我解释一下吗:)

谢谢

4

2 回答 2

2
NSData *response = [NSURLConnection sendSynchronousRequest:…];

这不是一个 alloc/create/copy 方法,所以你不应该释放response.

NSString *theString = [[NSString alloc] initWithData:…];

但你应该释放theString.

于 2010-04-25T06:01:53.850 回答
2

您正在释放一个您不拥有的对象;因为“sendSynchronousRequest”在其名称中既不包含单词“alloc”也不包含单词“copy”,你知道它给你的任何对象都将自动被“autorelease”释放,所以你所拥有的实际上是一个double -删除。

于 2010-04-25T06:02:51.440 回答