简体   繁体   English

不能使用大尺寸的QByteArray作为QString

[英]Can't use big size QByteArray as QString

I have big char* object. 我有很大的char*对象。 When I write this array to file, the size of this file is 63.7 MB. 当我将此数组写入文件时,此文件的大小为63.7 MB。 I receive QByteArray object from this object like this 我从这样的对象接收QByteArray对象

QByteArray outputDataByteArray(outputData, outputSize);

If I try to send this object to function as QString parameter, I receive 如果我尝试发送此对象以用作QString参数, QString收到

Invalid parameter passed to C runtime function. 无效的参数传递给C运行时函数。

While debugging I noticed that it crash after calling function with this array as input parameter, but does not come in this function. 在调试时,我注意到以该数组作为输入参数调用函数后,它崩溃了,但是没有出现在此函数中。

I tried something like this 我尝试过这样的事情

outputDataByteArray.append("\0");

according to documentation information, that 根据文档信息,

Warning: A byte array created with fromRawData() is not null-terminated, unless the raw data contains a 0 character at position size. 警告:使用fromRawData()创建的字节数组不能以空值结尾,除非原始数据在位置大小处包含0字符。 While that does not matter for QDataStream or functions like indexOf() , passing the byte array to a function accepting a const char * expected to be '\\0' -terminated will fail. 虽然这对于QDataStream或诸如indexOf()类的函数无关紧要,但是将字节数组传递给接受预期为'\\0'const char *的函数将失败。

But it didn't help me. 但这并没有帮助我。

On small size of data all work fine. 在小数据量上,一切正常。

Edition: 版:

This is some function I receive QBytearray in it (all work fine): 这是我在其中接收到QBytearray一些函数(一切正常):

uncompress(const QByteArray &inputByteArray, bool *ok)
{
if (ok != NULL)
    *ok = false;

const char *inputData = inputByteArray.data();

char *outputData = NULL;
qint64 outputSize   = 0;

for (int i = 0; i < CHUNK_COUNT_MAX; i++)
{
    const int outputSizeMax = CHUNKSIZE * (i + 1);
    outputData = new char[outputSizeMax];


    outputSize = LZ4_decompress_safe_withPrefix64k(inputData, outputData, inputByteArray.length(), outputSizeMax);

    if (outputSize < 0)
    {
        delete [] outputData;
        outputData = NULL;
    }
    else
    {
        break;
    }
}

if (outputData == NULL)
    return QByteArray();

QFile file("C:/Users/HOME/file.txt");
if(file.open(QIODevice::WriteOnly))
{
    file.write(outputData, outputSize);
}
file.close();

QByteArray outputDataByteArray(outputData, outputSize);

outputDataByteArray.append("\0");

delete [] outputData;

if (ok != NULL)
    *ok = true;

return outputDataByteArray;

} }

But, if I try to to call another function with this array as parameter: 但是,如果我尝试使用此数组作为参数调用另一个函数:

parse(outputDataByteArray, ok);

I receive what I wrote above. 我收到上面写的内容。

Addition: It does not come in this function but defination of function that i send QByteArray as parameter: 另外:它不属于此功能,但我将QByteArray作为参数发送给功能的定义:

parse(const QString &json, bool &success)
{
    success = true;
    if(!json.isNull() || !json.isEmpty())
    {
            QString data = json;

            int index = 0;


            QVariant value = Json::parseValue(data, index, success);


            return value;
    }
    else
    {
        return QVariant();
    }
}

You are calling LZ4_decompress_safe_withPrefix64k with: 您正在使用以下命令调用LZ4_decompress_safe_withPrefix64k

char *outputData = NULL;

And, this function's definitions is like this: 并且,此函数的定义如下所示:

int LZ4_decompress_safe_withPrefix64k(const char* source, char* dest, int compressedSize, int maxOutputSize)
{
    return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, endOnInputSize, full, 0, withPrefix64k, (BYTE*)dest - 64 KB, NULL, 64 KB);
}

And, the definition of LZ4_decompress_generic doesn't have any allocation code for your output buffer. 而且, LZ4_decompress_generic的定义没有输出缓冲区的任何分配代码。 See definition of LZ4_decompress_generic . 请参阅LZ4_decompress_generic 定义

It is simply typecasted in line # 1129 : 它只是在line # 1129类型转换:

BYTE* op = (BYTE*) dest;

And, it is further used throughout the definition of this function eg memcpy , LZ4_wildCopy , LZ4_copy8 , etc. 并且,在该函数的整个定义中都进一步使用了它,例如memcpyLZ4_wildCopyLZ4_copy8等。

So, it's invalid memory that is being pointed by your output buffer pointer. 因此,输出缓冲区指针指向的是无效的内存。 You need to allocate its memory before passing it to the function. 您需要先分配其内存,然后再将其传递给函数。 In addition to it, you need validate a pointer before passing it to other function. 除此之外,您还需要先验证指针,然后再将其传递给其他函数。 You may use asserts or if checks to validate this. 您可以使用asserts或者if检查来验证这一点。

Hope that would help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM