In code line
->NSInteger len = [(NSInputStream *)stream read:buf maxLength:1024];
I am getting very huge value of len from this method like:(18446744073709551615)
and crashes
Terminating app due to uncaught exception 'NSMallocException', reason: -[NSConcreteMutableData appendBytes:length:]: unable to allocate memory for length (18446744073709551615)
case NSStreamEventHasBytesAvailable:
{
NSMutableData* lobjReadData = [[NSMutableData alloc] init];
NSNumber* lnumBytesRead;
uint8_t buf[1024];
NSUInteger lintReadingBufferLength = 0;
NSUInteger lintTotalBufferReadedlength = 0;
NSUInteger lintPreviousBufferReadedlength = 0;
NSUInteger lintSeenIndex = 0;
while ([(NSInputStream*)stream hasBytesAvailable])
{
lintReadingBufferLength = [(NSInputStream *)stream read:buf
maxLength:1024];
// some times i am getting very huge vaqlue of lintReadingBufferLength like
//18446744073709551615
//crashes here with crash log -> Terminating app due to uncaught exception 'NSMallocException', reason: '*** -[NSConcreteMutableData appendBytes:length:]: unable to allocate memory for length (18446744073709551615)'
lintTotalBufferReadedlength += lintReadingBufferLength;
if(lintReadingBufferLength)
{
[lobjReadData appendBytes:(const void *)buf
length:lintReadingBufferLength];
// bytesRead is an instance variable of type NSNumber.
lnumBytesRead = [NSNumber numberWithInteger:
[lnumBytesRead integerValue]+lintReadingBufferLength];
NSArray* larrayOfBytes = [self arrayOfBytesFromData:lobjReadData];
for (NSInteger lintIndexCounter = lintPreviousBufferReadedlength; lintIndexCounter < lintTotalBufferReadedlength;
lintIndexCounter++)
{
NSObject* lobjByte = [larrayOfBytes objectAtIndex:lintIndexCounter];
NSString* lstrMessage = [NSString stringWithFormat:@"%@",lobjByte];
//doing some stuff here
}
lintPreviousBufferReadedlength = lintTotalBufferReadedlength;
}
else if(0 == lintReadingBufferLength)
{
}
else
{
SLog(@"no buffer!");
}
}
// SLog(@"--------------------------------------");
break;
}
18446744073709551615
is 0xffffffffffff
which is the maximum unsigned 64-bit integer value but it's also the equivalent of -1
as a 64-bit signed integer.
If you look at the reference for [NSInputStream read:maxLength:]
it says:
Return Value
A number indicating the outcome of the operation:
A positive number indicates the number of bytes read;
0 indicates that the end of the buffer was reached;
A negative number means that the operation failed.
So the operation failed and you are viewing the value as an unsigned value.