[NSMutableURLRequestreleased]:消息发送到已释放的实例

发布于 2024-12-03 06:30:53 字数 2682 浏览 0 评论 0原文

我的应用程序正在 didViewLoad 方法中调用下面的 rqst_run 方法,但出现错误。调试器报告以下错误:

[NSMutableURLRequest released]: message sent to deallocated instance

我不知道这个变量在哪里被释放

在头文件(接口部分)中声明:

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

并且我在实现中定义了这个方法:

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

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=%@¶m2=%@¶m3=%@",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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨曦÷微暖 2024-12-10 06:30:53

您的初始化

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

是一个方便的构造函数,它具有针对您使用构造函数初始化的对象的内置自动释放功能。因此,通过调用

[http_request release];

您正在尝试释放自动释放的东西。换句话说,你过度释放了。

您应该只对使用关键字分配的对象调用release,

“新建”、“分配”、“复制”。或使用“保留”

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"

简单爱 2024-12-10 06:30:53

[http_request release]; 是不必要的,因为您的对象是自动释放的。

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

一梦浮鱼 2024-12-10 06:30:53

为了创建 NSMutableURLRequest,您使用了返回 aurora 缓解实例的方法,并且您不负责它的发布。如果您使用的方法需要执行-alloc-copy-retain,则您有责任释放此实例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文