0

My app is calling the rqst_run method below in didViewLoad method but I've an error. Debugger reports the following error:

[NSMutableURLRequest released]: message sent to deallocated instance

I don't know where this variable get released

Declared in header file (interface section):

NSMutableString        *rqst_error;
NSMutableData      *rqst_data;
NSMutableDictionary    *listing_items;

and I defined this method in implementation:

- (void)rqst_run
{
    rqst_data = [[NSMutableData data] retain];
    NSMutableURLRequest *http_request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.feedserver.com/request/"]];
    [http_request setHTTPMethod:@"POST"];
    NSString *post_data = [[NSString alloc] initwithFormat:@"param1=%@&param2=%@&param3=%@",rqst_param1,rqst_param2,rqst_param3];
    [http_request setHTTPBody:[post_data dataUsingEncoding:NSUTF8StringEncoding]];
    rqst_finished = NO;
    [post_data release];
    NSURLConnection *http_connection = [[NSURLConnection alloc] initWithRequest:http_request];
    [http_request release];

    if(http_connection)
    {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

        if([rqst_data length]>0)
        {
            NSString *rqst_data_str = [[NSString alloc] rqst_data encoding:NSUTF8StringEncoding];   
            SBJsonParser *json_parser = [[SBJsonParse alloc] init];
            id feed = [json_parser objectWithString:rqst_data_str error:nil];
            listing_items = (NSMutableDictionary *)feed;
            [json_parser release];
            [rqst_data_str release];
        }   
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Feed" message:@"No data returned" delegate:self cancemButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }
   }
   else
   {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Problem" message:@"Connection to server failed" delegate:self cancemButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
   }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [rqst_data setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [rqst_data appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [rqst_data release];
    [connection release];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [rqst_data release];
    [connection release];
    rqst_finished = YES;
}
Stephane
  • 4,978
  • 9
  • 51
  • 86

3 Answers3

3

Line [http_request release]; is unnecessary as your object is autoreleased.

Nekto
  • 17,837
  • 1
  • 55
  • 65
  • +1 for a quick answer. Sorry I posted my answer but found it as almost duplicate of yours. So I deleted my answer. – Parth Bhatt Sep 06 '11 at 08:21
  • Not a problem. You can undelete your answer! – Nekto Sep 06 '11 at 08:22
  • No that would be unfair to you as then I would be posting a duplicate answer and more importantly it would be unfair to you. As you are the only deserving candidate for this answer to be selected as expected. – Parth Bhatt Sep 06 '11 at 08:25
3

Your initialization

NSMutableURLRequest *http_request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.feedserver.com/request/"]];

is a convenience constructor that has a built-in autorelease for the object you are initializing using the constructor. So by calling

[http_request release];

you are trying to release something that is auto-released. In other words, you are over-releasing.

You should only call release on objects that you allocate using the keywords,

"New", "Alloc", "Copy". or use "retain"

SayeedHussain
  • 1,696
  • 16
  • 23
0

For creation of your NSMutableURLRequest you used method which returns aurora eased instance and you not responsible for it's release. If you use methods which require you perform -alloc, -copy, -retain than you responsible for releasing this instance.

Serhii Mamontov
  • 4,942
  • 22
  • 26
  • Please don't create duplicate answers. Nekto was the first one to answer and your answer is not much different logically than Nekto's answer – Parth Bhatt Sep 06 '11 at 08:23
  • I didn't, we wrote them at the same time and on iPad I didn't noticed what someone posted the answer. – Serhii Mamontov Sep 06 '11 at 08:27
  • There is difference of 7 mins and so you mean to say you took 7 mins to write 3 lines? Please be fair to Nekto :) – Parth Bhatt Sep 06 '11 at 08:32
  • I don't even understand why I'm excuses before you, I wrote what was in my situation (sitting in the taxi and riding to the work and typing answers on iPad). Thats your deal but don't blame people, you don't known nothing in what condition people leaved response. P.S. this site is not chat. – Serhii Mamontov Sep 06 '11 at 08:56