Results 1 to 5 of 5

Thread: QByteArray and runtime error

  1. #1
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QByteArray and runtime error

    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:

    Qt Code:
    1. try{
    2. //my code
    3. }
    4. catch (std::exception& e){
    5. m_logger->Error("A critical error has occred. Error Code: " + QString::fromStdString(e.what()));
    6. }
    7. catch (...){
    8. qDebug() << "error"
    9. }
    To copy to clipboard, switch view to plain text mode 

    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 )

    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:

    Qt Code:
    1. QString post_request = postRequest;
    2. if(!post_request.isEmpty()){
    3. QStringList req = post_request.split("&");
    4. foreach(QString line, req){
    5. pr += "Content-Disposition: form-data; name=\"" + line.split('=')[0] + "\"\r\n\r\n";
    6. pr += line.split('=')[1] + "\r\n";
    7. pr += "--" + boundary + "\r\n";
    8. }
    9. data += pr;
    10. }
    11. data += file; //It crashes here
    12. data += "\r\n";
    13. data += QString("--" + boundary + "--\r\n").toLatin1();
    14. data += "\r\n";
    To copy to clipboard, switch view to plain text mode 

    "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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QByteArray and runtime error

    How have you populated "file"?

  3. #3
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QByteArray and runtime error

    1.)

    Generating file (mFile)
    Qt Code:
    1. QFile file(m_currentFile->filename);
    2. if(!file.open(QIODevice::ReadOnly)){
    3. Logger::getInstance()->Error(file.errorString());
    4. }
    5. if(!file.seek(segment_data->position)){
    6. Logger::getInstance()->Error(file.errorString());
    7. }
    8.  
    9. mFile = file.read(segment_data->segmentSize);
    To copy to clipboard, switch view to plain text mode 

    2.)

    putting mFile to sendFile function
    Qt Code:
    1. //code
    2.  
    3. strPage = m_httpClient->sendFileMultipart("url address" , request.toLatin1(), "att", mFileName, mFile);
    To copy to clipboard, switch view to plain text mode 

    2.
    //send file function
    Qt Code:
    1. QString HttpClient::sendFileMultipart(const QString &url, const QByteArray &postRequest, const QString &fileInputName, const QString &fileName, const QByteArray &file){
    2.  
    3. ///my code here
    4.  
    5. QString post_request = postRequest;
    6. if(!post_request.isEmpty()){
    7. QStringList req = post_request.split("&");
    8. foreach(QString line, req){
    9. pr += "Content-Disposition: form-data; name=\"" + line.split('=')[0] + "\"\r\n\r\n";
    10. pr += line.split('=')[1] + "\r\n";
    11. pr += "--" + boundary + "\r\n";
    12. }
    13. data += pr;
    14. }
    15. data += file; //It crashes here
    16. data += "\r\n";
    17. data += QString("--" + boundary + "--\r\n").toLatin1();
    18. data += "\r\n";
    19.  
    20. //code
    21. }
    To copy to clipboard, switch view to plain text mode 

    //Sorry I messed up file is not in base64 ..
    But, I still dont know what makes crash
    Last edited by #Dragon; 12th December 2017 at 21:29.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,532
    Thanked 281 Times in 276 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QByteArray and runtime error

    For me these line can be the source of the problem :
    Qt Code:
    1. pr += line.split('=')[1] + "\r\n";
    To copy to clipboard, switch view to plain text mode 
    You assume that the line always contains at least one = sign. And what if he is not there ?

  5. #5
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QByteArray and runtime error

    Yeah. I`ve checked this many times before... It's ok.

Similar Threads

  1. Runtime error
    By franco.amato in forum Qt Programming
    Replies: 8
    Last Post: 9th February 2011, 19:38
  2. Replies: 1
    Last Post: 25th September 2010, 08:20
  3. Runtime Error
    By zawal in forum Qwt
    Replies: 10
    Last Post: 1st August 2009, 11:46
  4. QT 4.4 runtime error
    By Ricochet in forum Newbie
    Replies: 1
    Last Post: 5th May 2008, 18:50
  5. runtime error
    By mickey in forum General Programming
    Replies: 1
    Last Post: 11th June 2006, 13:38

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.