Objc EXC_BAD_ACCESS when setting a NSString equal to another

别等时光非礼了梦想. 提交于 2019-12-22 10:29:04

问题


I'm having some really REALLY weird issues with NSString. When I read from an input stream and convert the data to a string I'm not able to set anything equal to that string. Here's the code:

NSString *name = r.URL.lastPathComponent;
NSString *data;
NSInputStream *stream = r.HTTPBodyStream;
uint8_t byteBuffer[1];
[stream open];

if (stream)
{
    // Get the request body from the stream. Used for setting the file name
    if (stream.hasBytesAvailable)
    {
        NSInteger bytesRead = [stream read:byteBuffer maxLength:4096];
        NSString *temp = [[NSString alloc] initWithBytes:byteBuffer length:bytesRead encoding:NSUTF8StringEncoding];

        data = temp; // EXC_BAD_ACCESS thrown here
    }
}

I need to copy the string over to another string but I can't. Does anyone know why this is happening?


回答1:


Your byte buffer is one byte big but you're reading 4096 bytes into it. This is likely to trigger a cascading sequence of events that culminates into the crash.



来源:https://stackoverflow.com/questions/41293656/objc-exc-bad-access-when-setting-a-nsstring-equal-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!