1

I am using NSInputStream to upload media file to server in following way.

uploadInputStream = [[NSInputStream alloc] initWithFileAtPath:videoFilePath];
uploadInputStream.delegate = self;
[uploadInputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[uploadInputStream open];

And in NSStream delegate method stream:handleEvent: i am fetching chunk of media file and uploading to server.

- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode {
switch (eventCode) {
    case NSStreamEventOpenCompleted:
        NSLog(@"Strem opened");
        break;

    case NSStreamEventHasBytesAvailable: {
        uint8_t buf[1024*1024];
        unsigned int len = 0;
        len = [(NSInputStream *)aStream read:buf maxLength:1024*1024];
        if(len)
        {
            @autoreleasepool {
                NSMutableData *fileData = [NSMutableData data];
                [fileData appendBytes:(const void *)buf length:len];
                [self uploadVideo:fileData];
            }
        }
        break;
    }

    case NSStreamEventHasSpaceAvailable:
        break;

    case NSStreamEventEndEncountered: {
        break;
    }

    case NSStreamEventErrorOccurred:
        break;

    case NSStreamEventNone:
        break;

    default:
        break;
}

}

So far so good and everything works fine in simulator. The issue is if i test this same code in real device (iPad-mini for now), it always crashing the application with EXC_BAD_ACCESS code=1 at strating of the delegate method stream:handleEvent: .

Anyone has any idea about this? Any help will be appreciated.

Thanks, Jay Stepin.

Jay Pandya
  • 507
  • 6
  • 26
  • Reduce buf size to see if it helps (64x64, etc) – user523234 Jan 19 '15 at 14:35
  • Could you check if eventCode is possible nil? or Could you comment the whole code in "stream:handleEvent" to see if the error might be in this method? – MB_iOSDeveloper Jan 19 '15 at 15:25
  • 1
    Well, its showing me error on line - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode it self. if i put NSLog in side the method in very first line, its not reaching up there. Still i will check the possibilities you have mention here. Thanks. – Jay Pandya Jan 20 '15 at 02:43
  • Thanks @user523234. Buffer size was the issue. If you write as an answer i will accept that and it would be help for others who are facing this issue. Can any one explain why it is not working for 1023*1024 buffer size and only in real device? Thanks for your help again. Jay Stepin. – Jay Pandya Jan 20 '15 at 02:58
  • I wished I could take credit for this but: http://stackoverflow.com/questions/21956559/nsinputstream-readmaxlength-throws-an-exception-saying-length-is-too-big-b – user523234 Jan 20 '15 at 14:09

0 Answers0