I use an NSInputStream
to read data from a file. It will crash if maxLength
is greater than 49152.
When it crashes -- sometimes, but not every time, it gives this message:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSConcreteData initWithBytes:length:copy:freeWhenDone:bytesAreVM:]: absurd length: 4294967295, maximum size: 2147483648 bytes'
From my calculation, 524288 is still less than that maximum, and can fit in the return value. What did I miss?
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
switch (eventCode)
{
case NSStreamEventHasBytesAvailable:
{
NSInteger bufferSizeNumber = 524288; //this one will crash.
// NSInteger bufferSizeNumber = 491520; // this one will work.
uint8_t buf[bufferSizeNumber];
unsigned int len = 0;
len = [_stream read:buf maxLength:bufferSizeNumber]; //crashing at this line
// more code ...
}
// more code...
}
}
Edit: (I think this is critical part of that behavior)
If I "start" in the background thread then the bufferSizeNumber behaves as described above. But if I "start" in the main thread, the bufferSizeNumber can go upto 943713 before it crashes.
- (void)start
{
_stream.delegate = self;
[_stream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[_stream open];
[[NSRunLoop currentRunLoop] run];
}