【问题标题】:[(NSInputStream *)stream read:buf maxLength:1024]; returns very huge value[(NSInputStream *)流读取:buf maxLength:1024];返回非常巨大的价值
【发布时间】:2015-04-29 06:28:07
【问题描述】:

在代码行中

->NSInteger len = [(NSInputStream *)stream read:buf   maxLength:1024]; 

我从这种方法中获得了非常巨大的 len 价值,例如:(18446744073709551615)

然后崩溃

由于未捕获的异常“NSMallocException”而终止应用程序,原因:-[NSConcreteMutableData appendBytes: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;

}

【问题讨论】:

    标签: ios sockets nsinputstream


    【解决方案1】:

    184467440737095516150xffffffffffff,这是最大的无符号 64 位整数值,但它也相当于 -1 作为 64 位有符号整数。

    如果您查看[NSInputStream read:maxLength:] 的参考,它会说:

    返回值

    表示操作结果的数字:

    • 正数表示读取的字节数;

    • 0表示到达缓冲区的末尾;

    • 负数表示操作失败。

    因此操作失败,您正在将值视为无符号值。

    【讨论】:

    • @user4388479 不,这是网络故障,它们总是发生。它只是意味着连接终止或类似的。
    • 不要使用NSUInteger 来跟踪响应,因为响应不是未签名的...
    【解决方案2】:

    read: 方法的返回类型是什么?是 NSUInteger 吗?它不是。它是 NSInteger。那么为什么它返回一个有符号整数而不是无符号整数呢?那在 read: 方法的文档中。阅读该文档,然后您应该知道不合理的大数字实际上是使用 NSUInteger 而不是 NSInteger 创建的您的代码中的错误。

    【讨论】:

    • 哇,我还以为我很直率。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    相关资源
    最近更新 更多