cURL / Mailing Lists / curl-library / Single Mail

curl-library

New with C++ and libcurl; getting massive memory leak

From: Torch <torch_at_torchsdomain.com>
Date: Tue, 3 Apr 2007 00:40:25 +0200

Before I go on, I wanted to send this to C++ mailing list, but the site for
subscription seems to be down at the moment.

I'm a professional webdeveloper (working mainly on Web2.0 projects now, with
PHP as server-side backbone) and was recently hired to do a pretty big
project. The reason I started learning C++ (it was a reasonable choice since
PHP and JS are based on it's syntax) is that the users will also need to be
able to keep updated about certain parts of the content on the site with
desktop application.

Anyway, I've read a couple of tutorials, messed around with various examples
and in a little while I've came up with a tiny test program just to see how
libcurl works in C++. And it behaves as intended, but the problem is I'm
getting a massive memory leak.
Here is the code:

#include "main.h"

static char error_buffer[CURL_ERROR_SIZE];
static string buffer;
static int writer(char *data, size_t size, size_t nmemb, string *buffer){
      int result = 0;
      if(buffer != NULL){
          buffer->append(data, size * nmemb);
          result = size * nmemb;
      }
      return result;
}

Form Main(PROJECT_NAME " v" PROJECT_VERSION, 100, 100, 175, 150);
TextBox siteurl("", AUTO_ID, 10, 15, 150, 25, Main);
Button check("Check", AUTO_ID, 40, 50, 50, 40, Main);

FormProcedure MainProcedures(FormProcArgs){

      CURL *curl;
      CURLcode result;
      curl = curl_easy_init();
      char* urlcheck;

  ON_CLOSE(){
   Application.close();
  }

  ON_COMMAND_BY(check){
         urlcheck = siteurl.getText();
         if(curl && strlen(urlcheck) > 1){
             check.disable();
             curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer);
             curl_easy_setopt(curl, CURLOPT_URL, urlcheck);
             curl_easy_setopt(curl, CURLOPT_HEADER, 0);
             curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
             curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
             curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
             result = curl_easy_perform(curl);
             if(result == CURLE_OK && buffer.size() > 0){
                 Main.msgBox("Site is working properly.", PROJECT_NAME);
             }
             else{
                 Main.msgBox(siteurl.getText() + " appears not to be
working.\n\n" + error_buffer, PROJECT_NAME);
             }
         }
         else{
             Main.msgBox("You have to enter site address.", PROJECT_NAME);
         }
         check.enable();
         curl_easy_cleanup(curl);
         curl_free(curl);
      }
  return 0;
}

rad_main()

  Main.procedure = MainProcedures;

rad_end()

As you see, I'm also using RADC++, which is used mainly for easier win forms
creation (the problem is not caused by it, I've allready tested it).
In the time I was writing this, I made the following modification:

FormProcedure MainProcedures(FormProcArgs){

      CURL *curl;
      CURLcode result;

      char* urlcheck;

  ON_CLOSE(){
   Application.close();
  }

  ON_COMMAND_BY(check){
         urlcheck = siteurl.getText();
         if(curl && strlen(urlcheck) > 1){
             check.disable();
             curl = curl_easy_init();
             curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer);
             curl_easy_setopt(curl, CURLOPT_URL, urlcheck);
             curl_easy_setopt(curl, CURLOPT_HEADER, 0);
             curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
             curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
             curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
             result = curl_easy_perform(curl);
             curl_easy_cleanup(curl);
             curl_free(curl);

and the memory usage has went down immensly, namely now it just increases by
5 to 75kb on each request instead of 1 to 5mb. But this also doesn't look
like a very nice solution, so I would grately apriciate if someone could
give me some advices (or examples) on how to keep memory usage as low as
possible since I'm on a tight schedule and can't spend much time
experimenting.

Big thanks ahead,
Vladan
Received on 2007-04-03