cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: about the bug of glibc detected free invalid pointer

From: yi xia <max.xiayi_at_gmail.com>
Date: Wed, 13 Feb 2008 17:09:11 +0800

Hi Dan,
I have done as your said, but the problem is still there. The code is pasted
here.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <sys/time.h>
#include <string>
#include <vector>
#include <pthread.h>
#include <signal.h>
#include <mcheck.h>

#define MAX_TIME_FOR_CONNECT 3

using std::string;
using std::vector;
string authToken;
bool volatile g_bEnd = 1;
size_t dataHandler(void *ptr, size_t size, size_t nmemb, void *arg)
{
        int retVal = 0;
        authToken.append((char *)ptr, size * nmemb);
        retVal = size*nmemb;
        return retVal;
}

void* handleLogin(void* me)
{
        curl_global_init(CURL_GLOBAL_ALL);
        string url = "http://www.xiayi345cheng.com";
        CURL *curl = curl_easy_init();
        CURLM *multi_handle ;
        time_t tp1;
        time_t tp2;
        long responseCode = 0;
        long mytimeout = MAX_TIME_FOR_CONNECT;

        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, mytimeout);
        //curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, mytimeout);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, dataHandler);
        authToken.clear();

        CURLcode success = curl_easy_perform(curl);
        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
        curl_easy_cleanup(curl);
        curl_global_cleanup();
        g_bEnd = 0;
}

int main()
{
        //mtrace();
        pthread_t threadId;
        pthread_create(&threadId,NULL,handleLogin,NULL);
        pthread_detach(threadId);
        while(g_bEnd)
        {
                ;//sleep(1);
        }
        return 0;
}

Thanks

2008/2/7, Dan Fandrich <dan_at_coneharvesters.com>:
>
> On Wed, Feb 06, 2008 at 10:05:17PM +0800, yi xia wrote:
> > Hi guys,
> > I feel puzzled about one error in using libcurl 7.17.1. To be simply,
> the
> > problem is that if getaddrinfo get timeouted and if the program has more
> than
> > one thread, then libcurl is ended with segmentation,
> > *** glibc detected *** ./testcurl: free(): invalid pointer: 0xb7fc9df4
> ***
> > Segmentation fault
>
> For one thing, you're not calling curl_global_init from only the original
> thread, as documented that you must. I doubt that's what's causing the
> problem, though, since you only have a single thread that ever executes
> a libcurl function. Try using glibc's memory integrity and tracing
> features to trace the bad free--look up mtrace and mcheck and set up
> your program. Also, if you insist on polling on a global flag as a
> thread synchronization method, you should be making g_bEnd volatile.
>
> >>> Dan
> --
> http://www.MoveAnnouncer.com The web change of address
> service
> Let webmasters know that your web site has moved
>
Received on 2008-02-13