gpt4 book ai didi

iOS : malloc: *** error for object 0x6e78580: incorrect checksum for freed object

转载 作者:行者123 更新时间:2023-11-28 17:37:16 37 4
gpt4 key购买 nike

我有以下方法:

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

最佳答案

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

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

这可能会发生,因为您传递给方法的变量已被释放且无效。

附加信息:

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

关于iOS : malloc: *** error for object 0x6e78580: incorrect checksum for freed object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9482939/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com