ios:malloc:***对象0x6e78580的错误:释放对象的校验和不正确

时间:2012-02-28 13:38:54

标签: ios malloc nsurlconnection

我有以下方法:

- (NSString*) make_sychronous_POST_request_with_payload:(NSData*) payload
{
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://xyz.com"]];
    [request setHTTPMethod:@"POST"];

    NSData *postData = [[Base64 encodeBase64WithData:payload] dataUsingEncoding:NSASCIIStringEncoding];
    [request setHTTPBody:postData];

    NSURLResponse* response = [[NSURLResponse alloc] init];
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    data = [Base64 decodeBase64WithString:[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]];

    return [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
}

但对于该行

NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

我收到此错误:

AppName(24163,0xa0c87540) malloc: *** error for object 0x6caf4b0: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
(gdb) up
#27 0x0006153b in -[FinTS30 checkForFinTS30BankWithURL] (self=0x6ca41a0, _cmd=0x9cdf8) at /path/to/project/AppName/FinTS30.m:72
72      NSString* answer = [self make_sychronous_POST_request_with_payload:message];
Current language:  auto; currently objective-c

我不明白为什么会这样。

(顺便说一句:这里我明确地希望使用不同步的同步请求。)

编辑: 好吧,这真的很奇怪。问题似乎是由postData对象引起的。这是我的代码的修改版本,不会崩溃

- (NSString*) make_sychronous_POST_request_with_payload:(NSData*) payload
{
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:bd.bankURL]];
[request setHTTPMethod:@"POST"];

NSData *postData = [[Base64 encodeBase64WithData:payload] dataUsingEncoding:NSASCIIStringEncoding];
[request setHTTPBody:postData];
[postData description]; //adding this prevents the code from crashing

NSURLResponse*  response = [[NSURLResponse alloc] init];
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
data = [Base64 decodeBase64WithString:[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]];

return [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
}

虽然这看起来很混乱但我经常测试它。如果我评论[postData description]代码崩溃,如果[postData description]被调用,一切正常。什么可能导致这种奇怪的行为?

1 个答案:

答案 0 :(得分:0)

您在项目中使用ARC吗?尝试使用__autoreleasing修饰符来响应。

NSURLResponse __autoreleasing * response = [[NSURLResponse alloc] init];

可能发生了,因为变量,你传递给方法,被释放并且无效。

其他信息:

In which situations do we need to write the __autoreleasing ownership qualifier under ARC?