developer
developer

Reputation: 5478

NSURLConnection help

Also I know that anything that I create using alloc I have to release it, but when I try to release 'request' object using [request release]; it throws the following error:

Program received signal:  “EXC_BAD_ACCESS”.
kill
error while killing target (killing anyway): warning: error on line 2179 of "/SourceCache/gdb/gdb-1510/src/gdb/macosx/macosx-nat-inferior.c" in function "macosx_kill_inferior_safe": (os/kern) failure (0x5x)

Upvotes: 0

Views: 336

Answers (2)

Anomie
Anomie

Reputation: 94794

You are creating the request using requestWithURL:cachePolicy:timeoutInterval:. Since that method name doesn't begin with "alloc" or "new" or contain "copy", according to the Memory Management Rules you don't own it and so should not be releasing it (unless you call retain on it explicitly, of course).

As for efficiency, the code seems fine. Note though that if the user can trigger a second load before the first completes that you will have problems; the solution to that is to either prevent such a thing or to save the NSURLConnection object created in load into an ivar and then have the pseudo-delegate methods check that against the passed connection before doing anything else. It's also a good idea to set the ivar to nil when you release the object it formerly contained, as then you cannot accidentally use the released object. And I note the variable name for your "authentication failed" alert is alertsuccess, that's misleading ;)

Upvotes: 2

Prabh
Prabh

Reputation: 2474

Try this in your load function:

    NSMutableURLRequest *request;

request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:pageUrl]];

NSURLResponse *response;
NSError *error = [[NSError alloc] init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];`

NSString *responseString = [NSString stringWithCString:[data bytes] length:[data length]]; `

Upvotes: -1

Related Questions