cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Re: CURLOPT_WRITEFUNCTION - cannot make it working...

From: Paolo Piacentini <paolopiace_at_hotmail.com>
Date: Sat, 4 Jun 2011 12:18:42 -0700

DS>> Really, you should then probably just go back to the working version and try again.
DS>> The problem is quite clearly that the logic in your code is broken.

Ok, I did it.

And, as with my code, I cannot make working getinmemory.c with its WriteMemoryCallback.

I hardly believe I'm doing something wrong since I just did cut/paste that in my code.

Therefore, I must ask to, please, take a look at this setup and try again your getinmemory.c, please.

I'm on libcurl/7.19.7 GnuTLS/2.8.5 zlib/1.2.3.3 libidn/1.15

Here's the setup. Code behaviour/results are explained in the comments.

#include <curl/curl.h>
#include <curl/easy.h>

struct MemoryStruct { // Global scope
        char *memory;
        size_t size;
};

// This is in main():
.....

curl_global_init(CURL_GLOBAL_ALL);

sessionA = curl_easy_init();

if(sessionA) {

        struct MemoryStruct chunk;
        chunk.memory = (char *)malloc(1);
        chunk.size = 0;

        curl_easy_setopt(sessionA, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
        curl_easy_setopt(sessionA, CURLOPT_WRITEDATA, (void *)&chunk);
        curl_easy_setopt(sessionA, CURLOPT_URL, UndrlURL.c_str());

        cout << chunk.size << " =chunk.size\t" << (void *)chunk.memory << " =chunk.memory\n";
        // (#) Here I see valid data: 0 =chunk.size, 0xa175418 =chunk.memory

        // Then, this calls WriteMemoryCallback
        //
        result = curl_easy_perform(sessionA);
.....

// The call back write function is just cut/paste from getinmemory.c:

static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
        size_t realsize = size * nmemb;
        struct MemoryStruct *mem = (struct MemoryStruct *)data;

        cout << (mem->size) << " =mem->size\t" << (void *)(mem->memory) << " =mem->memory\n";
        /*
        * At the very 1st call of WriteMemoryCallback I see INVALID data, different from (#):
        * 3079409664 =mem->size, 0xfbad2c84 =mem->memory
        * What changes these values??? They must be identical to (#).
        *
        * Then, the huge (mem->size) brings realloc in segmentation-fault ALL the times.
        * The code never executes beyond here.
        */
        mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);

        if (mem->memory == NULL) {
                /* out of memory! */
                printf("not enough memory (realloc returned NULL)\n");
                exit(EXIT_FAILURE);
        }

        memcpy(&(mem->memory[mem->size]), ptr, realsize);
        mem->size += realsize;
        mem->memory[mem->size] = 0;

        return realsize;
}
                                               

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2011-06-04