views:

289

answers:

2

I have a weird error that I cannot seem to find any documentation or posts for. When I try to connect to my web service (changed URL for privacy) using the standard textbook method, I receive EXC_BAD_INSTRUCTION or EXC_BAD_ACCESS on [NSURLConnection initWithRequest].

The weirdest thing is that on occasion I can step over the offending line without any exception, but 9 times out of 10 it causes this error. Any suggestions?

- (void)viewDidLoad {
    NSURL *url = [NSURL URLWithString:@"http://heres/where/my/webservice/url/is/"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];

    // cancel any old connection
    if(connection) {
        [connection cancel];
        [connection release];
    }

    // create new connection and begin loading data
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if(connection) {
        // if the connection was created correctly, release old data (if any), and alloc new
        [data release];
        data = [[NSMutableData data] retain];
    }

    [url release];
    [request release];
}

Any help or suggestions or RTFMs will be much appreciated!

+1  A: 

You are releasing autoreleased objects:

NSURL *url = [NSURL URLWithString:@"…"];
NSURLRequest *request = [NSURLRequest requestWithURL:url …];
// …
[url release];
[request release];

I think that Clang should be able to catch these, see Build → Build and Analyze (Cmd-Shift-A). Clang is your friend, get used to it.

zoul
Thanks zoul, I'm using Clang now. I removed the offending releases per your suggestion but get the same error.
Chris
Also, not sure if this helps or not but the offending line actually appears to be:NSURLRequest *theRequest=[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:60.0];And in the trace the last thing I can see is NSURLProtocolInit.
Chris
Zoul's answer is definitely correct, in that you are sending objects a `release` message, when you did not `-init` them. So you should remove those two lines, at least. Also, you should not release `data` unless it was `init`ialized in this or another method. If you release it before it is initialized, you will get an error.
Alex Reynolds
Chris: If you’re still having problems, I’d suggest the usual process of simplifying the code. Drop everything you don’t need, chunk by chunk, and if the problem suddenly disappears, you have found the offender.
zoul
Thanks Zoul and Alex. I've since removed the offending lines and still receive the error. I will try to start this over from scratch and see where I went wrong.
Chris
A: 

EXC_BAD_ACCESS means you are trying to release an object that has alreay been released. As zoul says, the url and request methods you use are autoreleasing methods so you shouldn't manually release them at the end of your code.

What you should do is accept his answer... because he's right.

imnk
I appreciate your help but the problem I was having was with EXC_BAD_INSTRUCTION. I have since removed the releases and still receive this error, but I'm accepting his answer since for the EXC_BAD_ACCESS he is definitely correct. Zoul, thanks for all your help.
Chris