【问题标题】:Problem with NSMutableURLRequestNSMutableURLRequest 的问题
【发布时间】:2010-02-20 21:11:00
【问题描述】:

我有以下方法

-(void)request
{
    responseData = [[NSMutableData data] retain];
    NSString *post =  [NSString stringWithFormat:@"id=%d&a_id=&d",1,1];
    NSLog(@"%@",post);
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    request = [[NSMutableURLRequest alloc] init];

    [request setURL:[NSURL URLWithString:kWebURL@"/req/request.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    [request setHTTPBody:postData];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

    [postLength release];
    [postData release];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [connection release];   
}

我的问题是 nsmutableurlrequest 对象存在泄漏。我想我应该在某个地方发布它,但是当我尝试这样做时,我得到了一个 exec_bad_access。无论我在哪里尝试释放它,我都会在 connectionDidFinishLoading 方法的末尾收到该错误。

编辑:看起来我可以释放 NSURLConnection 对象或 NSMutableURLConnection 对象。

如果我在应该删除它们的时候尝试删除它们,我会得到一个 exec_bad_access

【问题讨论】:

    标签: iphone


    【解决方案1】:

    我相信您的问题在于您发布的 postLength 和 postData。您没有分配其中任何一个,而是使用方便的方法创建了它们。你应该把这两行去掉,然后你应该可以释放你的 NSMutableURLRequest。

    【讨论】:

    • 是的,看起来它与崩溃有关。不知道我为什么要这样做...我在某个地方看到了发布的代码并且不假思索地使用了它...非常感谢!
    猜你喜欢
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 2014-07-14
    相关资源
    最近更新 更多