【问题标题】:ios : malloc: *** error for object 0x6e78580: incorrect checksum for freed objectios : malloc: *** 对象 0x6e78580 错误:释放对象的校验和不正确
【发布时间】:2012-03-17 23:36:37
【问题描述】:

我有以下方法:

- (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] 被调用,代码就会崩溃,一切正常。什么会导致这种奇怪的行为?

【问题讨论】:

  • 我假设您尝试通过 valgrind 运行它,对吧?
  • 当你设置它建议的断点时它会显示什么?

标签: ios malloc nsurlconnection


【解决方案1】:

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

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

由于变量的原因,您传递给方法可能会发生这种情况,被释放并且无效。

附加信息:

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

【讨论】:

    猜你喜欢
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 2014-03-10
    • 2013-11-19
    • 1970-01-01
    相关资源
    最近更新 更多