cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: How do I free the memory used by an in-memory, non-formdata post request? (HeapValidate exception)

From: Kamil Dudka <kdudka_at_redhat.com>
Date: Mon, 11 Oct 2010 22:55:31 +0200

On Monday 11 October 2010 22:31:29 Ola Mork wrote:
> This is an application error, not a libcurl-confusion (as Mr. Stenberg
> pointed out). I've attached a modified post-callback.c. The problem is
> in incrementing the position of the pointer in the callback. To free
> it, the pointer has to be moved back to its original location.
>
> The sample does this:
>
> pooh->readptr++
>
> so to free it I have to do this:
>
> pooh.readptr = pooh.readptr - strlen(data);
> free(pooh.readptr);

Why don't you just store the pointer you need to free later on?

diff --git a/post-callback.c b/post-callback.c
index 1274297..99db336 100644
--- a/post-callback.c
+++ b/post-callback.c
@@ -47,9 +47,12 @@ int main(void)

   struct WriteThis pooh;

+ char *ptr = strdup(data);
+ if(!ptr)
+ abort();
+
   pooh.sizeleft = strlen(data);
- pooh.readptr = (char *)malloc(pooh.sizeleft);
- memcpy(pooh.readptr, &data, pooh.sizeleft);
+ pooh.readptr = ptr;

   curl = curl_easy_init();
   if(curl) {
@@ -118,11 +121,7 @@ int main(void)

     /* always cleanup */
     curl_easy_cleanup(curl);
-
- pooh.readptr = pooh.readptr - strlen(data);
-
- printf("\n===address of pooh.readptr: %d\n", pooh.readptr);
- free(pooh.readptr);
+ free(ptr);
   }
   return 0;
 }
\ No newline at end of file
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-10-11