George Johnston
George Johnston

Reputation: 32258

Receiving EXC_BAD_ACCESS message with 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 *jsonString = [[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?

Upvotes: 1

Views: 10799

Answers (3)

Wolfgang Schreurs
Wolfgang Schreurs

Reputation: 11834

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).

Upvotes: 1

Alex
Alex

Reputation: 65942

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.

Upvotes: 2

skorulis
skorulis

Reputation: 4381

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

Upvotes: 5

Related Questions