cURL / Mailing Lists / curl-library / Single Mail

curl-library

about the bug of glibc detected free invalid pointer

From: yi xia <max.xiayi_at_gmail.com>
Date: Wed, 6 Feb 2008 22:05:17 +0800

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

with gdb, i get the following stack information.
Program terminated with signal 11, Segmentation fault.
#0 0x005e87e6 in ?? () from /lib/libgcc_s.so.1
(gdb) where
#0 0x005e87e6 in ?? () from /lib/libgcc_s.so.1
#1 0x005e96b2 in _Unwind_Backtrace () from /lib/libgcc_s.so.1
#2 0x003feb88 in backtrace () from /lib/libc.so.6
#3 0x0037aed1 in __libc_message () from /lib/libc.so.6
#4 0x00382f41 in _int_free () from /lib/libc.so.6
#5 0x00386580 in free () from /lib/libc.so.6
#6 0x003d2c50 in getaddrinfo () from /lib/libc.so.6
#7 0x00000001 in ?? ()
(gdb)

The source code is pasted as the following, the point is that if I do not
use thread, the segmentation error is never happened.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "xmlParser.h"
#include <curl/curl.h>
#include <curl/easy.h>
#include <sys/time.h>
#include <string>
#include <vector>
#include <pthread.h>
#include <signal.h>

#define MAX_TIME_FOR_CONNECT 3

using std::string;
using std::vector;
string authToken;
bool 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)
{
        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);
        g_bEnd = 0;
}

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

Anybody can give me some hints?

Thanks a lot in advance

Max
Received on 2008-02-06