释放NSMutableData时发生NSURLConnection崩溃 [英] NSURLConnection crash on releasing NSMutableData

查看:331
本文介绍了释放NSMutableData时发生NSURLConnection崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已遵循使用NSURLConnection ,有时(非常少见)我的项目在方法中崩溃。

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



当我尝试释放我的 NSMutableData
我想知道为什么会崩溃!



我使用的一些代码:

   - (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(@无启动连接);
}
}
}

- (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([ 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];
}
}
}


解决方案>

您的代码正在执行异步调用。每次调用start方法时,都将创建NSURLConnection对象的新实例,但对于数据只有一个对象(getData)。考虑一些如何有两个同时调用,当第一个失败它释放你的连接和getData对象,当第二个失败,它释放你的连接对象成功,但你的getData对象已经释放在以前的失败调用导致代码崩溃。 / p>

要解决这个问题,请在发布后将对象设置为nil,并在必要时执行nil检查。


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];
        }
    }
}

解决方案

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.

这篇关于释放NSMutableData时发生NSURLConnection崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆