开发者

Receiving EXC_BAD_ACCESS message with the following code

开发者 https://www.devze.com 2023-02-23 04:14 出处:网络
I am new to Objective C, so I\'m not really even sure what this message means: EXC_BAD_ACCESS When executing the following code:

I am new to Objective C, so I'm not really even sure what this message means:

EXC_BAD_ACCESS

When executing the following code:

-(void)HelloWorld
{
    NSURL *url = [NSURL URLWithString:@"http://example.com/service.asmx/HelloWorld"];
    NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL: url];

    //do post request for parameter passing 
    [request setHTTPMethod:@"POST"];

    //set the content type to JSON
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection release];
    [request release];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
    // Store incoming data into a string
    NSString *jso开发者_运维技巧nString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

I'm attempting to integrate with the Json Framework.

I call HelloWorld, which executes an async request to my service. What's strange, is that it worked one time, and now I get this EXC_BAD_ACCESS message every subsequent time. Any ideas what would be causing this?


You shouldn't be releasing the request. It's already autoreleased.


EXC_BAD_ACCESS means you have a bad pointer. In your case, it's because you are releasing the request when it's already autoreleased.


There's also another problem with your code (unrelated to the crash). You create an NSURLConnection and immediately release the connection after creation. When creating an asynchronous connection, you should release the connection in the delegate methods (if connection fails or if connection did finish loading).

0

精彩评论

暂无评论...
验证码 换一张
取 消