cURL / Mailing Lists / curl-library / Single Mail

curl-library

deallocation of MemoryStruct in callback

From: Jami Amore <jami.amore_at_fedex.com>
Date: Fri, 07 Mar 2003 15:29:01 -0700

I've written a program that uses part of the getinmemory.c example from
the libcurl website (I've pasted that example in below). I'm curious as
to why there isn't a free() anywhere the corresponds to the realloc() in
the WriteMemoryCallback. Is the deallocation of that memory taken care
of by curl_easy_cleanup()?

Thank you in advance for your help...

Jami Amore

                 19 #include <stdio.h>
                 20
                 21 #include <curl/curl.h>
                 22 #include <curl/types.h>
                 23 #include <curl/easy.h>
                 24
                 25 struct MemoryStruct {
                 26 char *memory;
                 27 size_t size;
                 28 };
                 29
                 30 size_t
                 31 WriteMemoryCallback(void *ptr, size_t size, size_t
nmemb, void *data)
                 32 {
                 33 register int realsize = size * nmemb;
                 34 struct MemoryStruct *mem = (struct MemoryStruct
*)data;
                 35
                 36 mem->memory = (char *)realloc(mem->memory,
mem->size + realsize + 1);
                 37 if (mem->memory) {
                 38 memcpy(&(mem->memory[mem->size]), ptr,
realsize);
                 39 mem->size += realsize;
                 40 mem->memory[mem->size] = 0;
                 41 }
                 42 return realsize;
                 43 }
                 44
                 45 int main(int argc, char **argv)
                 46 {
                 47 CURL *curl_handle;
                 48
                 49 struct MemoryStruct chunk;
                 50
                 51 chunk.memory=NULL; /* we expect realloc(NULL,
size) to work */
                 52 chunk.size = 0; /* no data at this point */
                 53
                 54 /* init the curl session */
                 55 curl_handle = curl_easy_init();
                 56
                 57 /* specify URL to get */
                 58 curl_easy_setopt(curl_handle, CURLOPT_URL,
"http://cool.haxx.se/");
                 59
                 60 /* send all data to this function */
                 61 curl_easy_setopt(curl_handle,
CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
                 62
                 63 /* we pass our 'chunk' struct to the callback
function */
                 64 curl_easy_setopt(curl_handle, CURLOPT_FILE, (void
*)&chunk);
                 65
                 66 /* get it! */
                 67 curl_easy_perform(curl_handle);
                 68
                 69 /* cleanup curl stuff */
                 70 curl_easy_cleanup(curl_handle);
                 71
                 72 /*
                 73 * Now, our chunk.memory points to a memory block
that is chunk.size
                 74 * bytes big and contains the remote file.
                 75 *
                 76 * Do something nice with it!
                 77 */
                 78
                 79 return 0;
                 80 }
                 81

-------------------------------------------------------
This SF.net email is sponsored by: Etnus, makers of TotalView, The debugger
for complex code. Debugging C/C++ programs can leave you feeling lost and
disoriented. TotalView can help you find your way. Available on major UNIX
and Linux platforms. Try it free. www.etnus.com
Received on 2003-03-07