im recieving a data through a steam which looks something like this:
Quote:
nameStartBLABLABLABLABLAnameEnd
or
Quote:
startTextBLABLABLABLABLAendText
When the data arrives I want to first check if it says "startName" or "startText" in the beginning and then use the data in between accordingly. The first string recieved is always the one with "startName". The app does recognize this and used the string correctly. But wehn the second string arrives I always get an error. Here is my code and the error:
Program received signal: “EXC_BAD_ACCESS”.
App(2953,0x39d802b8) malloc: *** error for object 0x164084: incorrect checksum for freed object - object was probably modified after being freed.
Where is the problem. I cant see where I am freeing twice!
if (stream == _inStream) {
NSInteger bytesRead;
uint8_t buffer[32768];
bytesRead = [_inStream read:buffer maxLength:sizeof(buffer)];
NSString *incoming = [[NSString alloc] initWithBytes:buffer length:bytesRead encoding:NSUTF8StringEncoding];
NSString *outputText = [[NSString alloc] init];
// This string is leaked later. Just do this:
NSString *outputText = nil;
if(!bytesRead) {
if ([stream streamStatus] != NSStreamStatusAtEnd)
[self _showAlert:@"Failed reading data from peer."];
}
// Do yourself a favor...
NSString *prefix = [incoming substringWithRange:NSMakeRange(0,9)];
if (bytesRead > 0 ) {
if ([prefix isEqualToString: @"nameStart"]) {
guestName = [incoming substringWithRange:NSMakeRange(9,([incoming length] -16))];
// Is guestName an instance variable? Use [set setGuestName:...] instead.
} else {
if ([prefix isEqualToString: @"startText"]) {
outputText = [[NSString alloc] initWithString: [incoming substringWithRange:NSMakeRange(9,([incoming length] -16))]];
// You've just leaked the string created previously. And, since you
// don't seem to be doing anything with it past this point, you're
// leaking the new one, too.
}
}
}
// incoming is leaked here since you haven't released it.
}
break;
I'm not seeing an over-release problem, but you are leaking.
2009-09-19 17:00:15.139 Texter[3096:207] Start 1
2009-09-19 17:00:15.147 Texter[3096:207] Start 2
Program received signal: “EXC_BAD_ACCESS”.
App(3096,0x39d802b8) malloc: *** error for object 0x15fab4: incorrect checksum for freed object - object was probably modified after being freed.
Yes, it will be run everytime the app recieves data.
I postet the code here. I hope this gives you a better overview!
Quote:
2009-09-19 17:00:15.139 Texter[3096:207] Start 1
2009-09-19 17:00:15.147 Texter[3096:207] Start 2
Program received signal: “EXC_BAD_ACCESS”.
App(3096,0x39d802b8) malloc: *** error for object 0x15fab4: incorrect checksum for freed object - object was probably modified after being freed.
1. Stop using instance variables directly. See the properties link in my signature for more information. Briefly, everything that you have that is _something should be self.something instead. I'm not kidding. Rewrite everything. Do it now. This is important regardless, but is doubly so in code that gets run a lot.
2009-09-19 21:33:10.454 Texter[3240:207] Start 1
2009-09-19 21:33:10.462 Texter[3240:207] Start 2
2009-09-19 21:33:10.478 Texter[3240:207] incoming is: startText
Program received signal: “EXC_BAD_ACCESS”.
Texter(3240,0x39d802b8) malloc: *** error for object 0x13b0f4: incorrect checksum for freed object - object was probably modified after being freed.