Results 1 to 5 of 5

Thread: libcurl under Windows does not write data after realloc

  1. #1
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default libcurl under Windows does not write data after realloc

    I'm trying to get a libcurl script to run under Windows. I know about QNetworkRequest, but I would just like to get this working, if possible. It works fine under Linux.

    For some reason, the first time the WriteMemoryCallback function is called, data is written as expected. However, on subsequent call to the same function, new data does not get appended to the array after a realloc. Any idea what is going on? Thanks for your help!

    Copying straight from the example provided by the libcurl folks:

    Qt Code:
    1. curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); // Rather than writing to a file, keep data in memory
    2. curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) {
    2.  
    3. size_t realsize = size * nmemb;
    4. struct MemoryStruct *mem = (struct MemoryStruct *)data;
    5.  
    6. mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
    7. if (mem->memory == NULL) {
    8. printf("not enough memory (realloc returned NULL)\n");
    9. exit(EXIT_FAILURE);
    10. }
    11.  
    12. memcpy(&(mem->memory[mem->size]), ptr, realsize);
    13. mem->size += realsize;
    14. mem->memory[mem->size] = 0;
    15.  
    16. // THIS IS CORRECT
    17. qDebug()<<"Bytes received:";
    18. qDebug()<<QString::fromUtf8((char *)ptr);
    19.  
    20. // NEW DATA DID NOT GET APPENDED!
    21. qDebug()<<"Bytes in memory:";
    22. qDebug()<<QString::fromUtf8(mem->memory);
    23.  
    24. return realsize;
    25. }
    To copy to clipboard, switch view to plain text mode 

    In my .h file, I have:
    Qt Code:
    1. struct MemoryStruct {
    2. char *memory;
    3. size_t size;
    4. };
    5. static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data);
    6.  
    7. class MainWindow : public QMainWindow
    8. {
    9. Q_OBJECT
    10. etc.
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: libcurl under Windows does not write data after realloc

    To be sure you don't have an issue with charachter encoding and similar related problems that just make the qDebug() string not visible, put a breakpoint on that line and look in to the mem->memory memory in the debugger.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: libcurl under Windows does not write data after realloc

    Thanks for your reply! You got me on the right track. This appears to be a display bug with Qt Creator. There is some sort of buffer in the debug output that causes a delay. The first thing I did was replace qDebug() with fprintf(stderr...), printing one character at a time. Doing this, I noticed that the output to each fprintf() call was truncated, but that subsequent calls to fprintf() completed the previous call's output. This does not happen using qDebug (it must clear the buffer each time).

    In any case, the good news is that the webpage does seem to be downloaded correctly, as verified by a) writing the output to a file b) converting to a QString and using a regular expression to look for the terminating </html>. The only problem is, I can't figure out how to display it within Qt Creator (using the Watch menu doesn't work either - all I see is a small image of a musical eighth note. Charming.).

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: libcurl under Windows does not write data after realloc

    Is this a console application (QCoreApplication) or a gui application (QApplication)?
    Are you in release or debug mode?
    Do you want the output only for debug purposses or do you want it as an application feature?
    qDebug() is only a debug output.
    If this is a GUI app you probably should output it to a widget, or if you really want it on the console, you should output it to std output.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: libcurl under Windows does not write data after realloc

    This is a GUI application. I'm in debug mode, and I just wanted to see the output for debugging. I finally found a simple workaround, which is to use split("\n") to break the webpage (one long string) into multiple lines, then use a loop to print each line. Thanks for your help!

Similar Threads

  1. using libcurl in QT with Mingw in Windows
    By ct in forum Qt Programming
    Replies: 18
    Last Post: 23rd January 2012, 01:41
  2. Read/write data from file
    By Insomnium in forum Qt Programming
    Replies: 5
    Last Post: 10th December 2010, 07:35
  3. How to write data into a file
    By grsandeep85 in forum Qt Programming
    Replies: 9
    Last Post: 21st July 2009, 08:51
  4. realloc
    By mickey in forum General Programming
    Replies: 1
    Last Post: 4th May 2008, 19:59
  5. TCP Write Raw data
    By ^NyAw^ in forum General Programming
    Replies: 19
    Last Post: 23rd November 2007, 16: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.