cURL / Mailing Lists / curl-library / Single Mail

curl-library

RE: about the bug of glibc detected free invalid pointer

From: <Florian.PONROY_at_fr.thalesgroup.com>
Date: Wed, 13 Feb 2008 10:26:48 +0100

Why not using pthread_join() instead of doing a fancy loop?

-----Message d'origine-----
De : curl-library-bounces_at_cool.haxx.se
[mailto:curl-library-bounces_at_cool.haxx.se]De la part de yi xia
Envoyé : mercredi 13 février 2008 10:09
À : libcurl development
Objet : Re: about the bug of glibc detected free invalid pointer

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
<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
<mailto: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 <http://www.MoveAnnouncer.com>
The web change of address service
          Let webmasters know that your web site has moved
Received on 2008-02-13