异步NSURLConnection引发EXC_BAD_ACCESS

She*_*lam 6 iphone xcode cocoa-touch objective-c nsurlconnection

我不确定为什么我的代码会抛出EXC_BAD_ACCESS,我遵循了Apple的文档中的指导原则:

-(void)getMessages:(NSString*)stream{

    NSString* myURL = [NSString stringWithFormat:@"http://www.someurl.com"];

    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:myURL]];

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) {
        receivedData = [[NSMutableData data] retain];
    } else {
        NSLog(@"Connection Failed!");
    }

}
Run Code Online (Sandbox Code Playgroud)

和我的代表方法

#pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // This method is called when the server has determined that it
    // has enough information to create the NSURLResponse.

    // It can be called multiple times, for example in the case of a
    // redirect, so each time we reset the data.

    // receivedData is an instance variable declared elsewhere.
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to receivedData.
    // receivedData is an instance variable declared elsewhere.
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    // release the connection, and the data object
    [connection release];
    // receivedData is declared as a method instance elsewhere
    [receivedData release];

    // inform the user
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data
    // receivedData is declared as a method instance elsewhere
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);

    // release the connection, and the data object
    [connection release];
    [receivedData release];
}
Run Code Online (Sandbox Code Playgroud)

我在didReceiveData上获得了一个EXC_BAD_ACCESS.即使该方法只包含NSLog,我也会收到错误.

注意:receivedData是我的头文件中的NSMutableData*

Man*_*ath 6

使用NSZombieEnabled断点并检查哪个是释放的对象.

还检查:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    if ([response expectedContentLength] < 0)
    {
        NSLog(@"Connection error");
            //here cancel your connection.
            [connection cancel];
        return;
    }
}
Run Code Online (Sandbox Code Playgroud)


Jer*_*myP 5

我遵循了Apple的文档中的指导原则:

事实并非如此.在以下两个中,您违反了规则:

- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
    // release the connection, and the data object
    [connection release];
    // receivedData is declared as a method instance elsewhere
    [receivedData release];

    // inform the user
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data
    // receivedData is declared as a method instance elsewhere
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);

    // release the connection, and the data object
    [connection release];
    [receivedData release];
}
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,您都不会connection使用alloc开头new或包含的方法获取对象copy.你不拥有connection这些方法.您不能在这些方法中释放它.

在我看来,你在那里发布了receiveData似乎有点狡猾.我建议您在释放后立即将实例变量设置为nil.

[receivedData release];
receivedData = nil;
Run Code Online (Sandbox Code Playgroud)

这样,它不会被意外释放莫过于一次.