1

I have followed the instruction from Using NSURLConnection and sometimes (very very seldom) my project crashes in method.

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

It crashes when i try to release my NSMutableData. I want to know why it crashes!

Some code I use:

- (void) start
{
    while (1)
    {
        NSString *stringURL = @"http://www.iwheelbuy.com/get.php?sex=1";
        NSURL *url = [NSURL URLWithString:stringURL];
        NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        if (connection)
        {
            getData = [[NSMutableData data] retain];
            break;
        }
        else
        {
            NSLog(@"no start connection");
        }
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    if ([connection.originalRequest.URL.absoluteString rangeOfString:@"get.php"].location != NSNotFound)
    {
        [getData setLength:0];
    }
}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    if ([connection.originalRequest.URL.absoluteString rangeOfString:@"get.php"].location != NSNotFound)
    {
        [connection release];
        [getData release];
    }
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    if ([connection.originalRequest.URL.absoluteString rangeOfString:@"get.php"].location != NSNotFound)
    {
        [connection release];
        NSString *html = [[NSString alloc] initWithData:getData encoding:NSASCIIStringEncoding];
        [getData release];
        if ([html rangeOfString:@"id1="].location != NSNotFound && [html rangeOfString:@"id2="].location != NSNotFound)
        {
            NSLog(@"everything is OKAY");
            [html release];
        }
        else
        {
            [html release];
            [self start];
        }
    }
}
iWheelBuy
  • 5,470
  • 2
  • 37
  • 71
  • 1
    Show the console output and crash report or stack trace from a crash. Also, run the app under the Zombies instrument. – Ken Thomases May 13 '12 at 08:04
  • I keep trying to get some crash report but with still with no success. I don't know what is Zombies instrument... – iWheelBuy May 13 '12 at 08:25
  • My app is running under Zombies, what should I do next? – iWheelBuy May 13 '12 at 08:43
  • You should exercise it. Put it through its paces, especially this part which is sometimes crashing. Instruments will tell you if your app tries to access a deallocated object. – Ken Thomases May 13 '12 at 08:44
  • It might be interesting. I have put NSLog in all connection methods. And twice I got an interesting log right before the crash in method: `- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"error: %@",connection.originalRequest.URL.absoluteString); }` The log was `error: (null)`. It means that the connection was released before the error method. I release the connection only in `connectionDidFinishLoading` and in `connection didFailWithError`. It means connection was finished and than failed... Thats weird! – iWheelBuy May 13 '12 at 18:11
  • You need to separately log `connection`, `connection.originalRequest`, and `connection.originalRequest.URL` to figure out which subexpression was `nil`. It might not have been `connection`. – Ken Thomases May 13 '12 at 18:15
  • I hope I'll find the problem. Thanks for support! – iWheelBuy May 13 '12 at 18:27

2 Answers2

1

Your code is performing asynchronous calls. Each time you call your start method you create new instance of NSURLConnection object but you have only one object (getData) for data. Considering some how there are two simultaneous calls and when first one failed it released your connection and getData object and when the second one fails it releases your connection object successfully but your getData object is already released in previous fail call causing your code to crash.

To fix this always set your objects to nil after you release them and perform nil check where necessary.

Sohaib
  • 10,941
  • 9
  • 32
  • 34
0

You need to release getData instead of myNSMutableData.

rishi
  • 11,779
  • 4
  • 40
  • 59
  • Ok. whether it happens sometimes only? – rishi May 13 '12 at 07:41
  • only sometimes! once from 100 "start" methods calls – iWheelBuy May 13 '12 at 07:46
  • have you noticed crash logs at that instance, whether NSLog(@"no start connection"); is present in logs at that time? – rishi May 13 '12 at 07:47
  • what i doubt now, if connection won't be successful then your getData is not retained but you are releasing that in didFail method, might be this will the reason for crash? – rishi May 13 '12 at 08:10
  • I have tried to connect withoun getData retained. Nothing happened and no crash. – iWheelBuy May 13 '12 at 08:15
  • @iWheelBuy with out retained and with release? – rishi May 13 '12 at 08:16
  • yes! the connection is successful. It checks the getData in `connectionDidFinishLoading` and transforms into text. then releases. in text it can't find "id1" and "id2" and again goes `[self start]` – iWheelBuy May 13 '12 at 08:24