PDA

View Full Version : QByteArray and runtime error



#Dragon
12th December 2017, 20:48
Hi, I've got a problem with appending data to QByteArray, sometimes it causes C++ Runtime Error...
I tried to debug, but it always fails, just shows message box with runtime error, after researching loads of code I found out that the error is bad_alloc in QByteArray

Code I used to detect error:



try{
//my code
}
catch (std::exception& e){
m_logger->Error("A critical error has occred. Error Code: " + QString::fromStdString(e.what()));
}
catch (...){
qDebug() << "error"
}


and also tried different ways like qDebug() << "1"; before and another one after my code ant it never showed second one so I'm really confident that the error is in that place
(I know that the way to detect doesn't look very professional, but it works :D)

So, lets jump to main problem...

I'm writing an app sending attachment and need to append QByteArray with data previously encoded to base64 to another QByteArray then create post request and send it to the server

Part of code:



QString post_request = postRequest;
if(!post_request.isEmpty()){
QStringList req = post_request.split("&");
QByteArray pr;
foreach(QString line, req){
pr += "Content-Disposition: form-data; name=\"" + line.split('=')[0] + "\"\r\n\r\n";
pr += line.split('=')[1] + "\r\n";
pr += "--" + boundary + "\r\n";
}
data += pr;
}
data += file; //It crashes here
data += "\r\n";
data += QString("--" + boundary + "--\r\n").toLatin1();
data += "\r\n";


"file" is QByteArray - file encoded to base64, it has no more than 20MiB

I don't understand why it crashes, as I know QByteArray can handle about 2GB of data
I also tried qbytearray.append()

Any ideas or suggestions ?
Thank You

ChrisW67
12th December 2017, 20:53
How have you populated "file"?

#Dragon
12th December 2017, 21:23
1.)

Generating file (mFile)


QFile file(m_currentFile->filename);
if(!file.open(QIODevice::ReadOnly)){
Logger::getInstance()->Error(file.errorString());
}
if(!file.seek(segment_data->position)){
Logger::getInstance()->Error(file.errorString());
}

mFile = file.read(segment_data->segmentSize);



2.)

putting mFile to sendFile function


//code

strPage = m_httpClient->sendFileMultipart("url address" , request.toLatin1(), "att", mFileName, mFile);

2.
//send file function

QString HttpClient::sendFileMultipart(const QString &url, const QByteArray &postRequest, const QString &fileInputName, const QString &fileName, const QByteArray &file){

///my code here

QString post_request = postRequest;
if(!post_request.isEmpty()){
QStringList req = post_request.split("&");
QByteArray pr;
foreach(QString line, req){
pr += "Content-Disposition: form-data; name=\"" + line.split('=')[0] + "\"\r\n\r\n";
pr += line.split('=')[1] + "\r\n";
pr += "--" + boundary + "\r\n";
}
data += pr;
}
data += file; //It crashes here
data += "\r\n";
data += QString("--" + boundary + "--\r\n").toLatin1();
data += "\r\n";

//code
}


//Sorry I messed up file is not in base64 ..
But, I still dont know what makes crash

Lesiok
13th December 2017, 07:01
For me these line can be the source of the problem :

pr += line.split('=')[1] + "\r\n";You assume that the line always contains at least one = sign. And what if he is not there ?

#Dragon
13th December 2017, 11:39
Yeah. I`ve checked this many times before... It's ok.