1

I have the following method:

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

But for the line

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

I get this error:

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

I do not understand why this happens.

(Btw: here I explicitly want to use synchronous request not asychronous.)

EDIT: Okay, well this is really really odd. The problem seems to be caused be the postData object. Here is a modified version of my code that does not crash

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

Although this seems quite confusing I tested it various times. If I comment [postData description] the code crashes if [postData description] gets called everything works fine. What could cause such a strange behavior?

toom
  • 12,864
  • 27
  • 89
  • 128

1 Answers1

0

Are you using ARC in your project ? Try to use __autoreleasing modifier to response.

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

It could be happened, because of variable, that you passed to method, was released and is invalid.

Additional information:

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

Community
  • 1
  • 1
tt.Kilew
  • 5,954
  • 2
  • 33
  • 51