cURL / Mailing Lists / curl-library / Single Mail

curl-library

memory leak using libcurl

From: Outlandish . <yahyai-0_at_hotmail.com>
Date: Tue, 12 Jul 2011 00:40:54 +0400

Hi
I have make libcur class cuz I want to use it many times....

curl..h
#include <iostream>
#include <string>
#include "curl/curl.h"
#include "curl/easy.h"
using namespace std;

class curlClass
{
    static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
    {
        size_t written;
        written = fwrite(ptr, size, nmemb, stream);
        return written;
    }

protected:
    CURL *curl;
    FILE *fp;
    CURLcode res;
    char *url;
    char *filename;

public:
    curlClass(char *url1 ,char*filename1);
    ~curlClass();

    void get();
};

curl.cc
#include "curl-class.h"
#include <stdlib.h>

curlClass::curlClass(char *url1, char*filename1)
 : url(url1)
 ,filename(filename1)
{
  }

curlClass ::~curlClass(){};

void curlClass::get()
{
    curl = curl_easy_init();

    if(curl) {
        fp = fopen(filename,"wb");
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        fclose(fp);
    }
    else
    {
        cerr<<"error curl";
        exit(1);
    }
}

In my function I used and it worked
curlClass test1(url,urlfilename);
test.get();

but when I used it in other function
curlClass test2(url,urlfilename);

test.get();

it give memory leak

thanks in advance

                                               

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