У многопоточного приложения curl есть проблемы с выделением памяти - PullRequest
0 голосов
/ 16 января 2011

Я работаю над приложением на C ++, которое создает потоки и передает несколько URL-адресов потоков для параллельной загрузки cURL.

Я использую метод, который должен быть безопасным для загрузки изображений и видео и т. Д. Я использую memcpy вместо того, чтобы предполагать, что данные представляют собой строку или массив символов.

Я передаю каждому потоку структуру thread_status для ряда вещей. Структура сообщает родительскому процессу, что поток завершен, загрузка. Он также хранит данные, загружаемые cURL, и отслеживает их размер, поскольку cURL возвращает больше буферов для записи.

Я передаю указатель (void *), который указывает на каждую структуру, выделенную при инициализации, каждому потоку, выполняющему загрузку. Первая страница загружена правильно, после этого я продолжаю получать ошибки от realloc ().

Вот самый простой пример, иллюстрирующий мою проблему. Этот пример не является многопоточным, но использует аналогичную структуру для отслеживания самого себя.

#include <string>
#include <assert.h>
#include <iostream>
#include <curl/curl.h>
#include <stdlib.h> 
#include <stdio.h>
#include <string.h>

#define NOT_READY   1
#define READY       0

using namespace std;

struct thread_status {
    int id;
  pthread_t *pid;
    int readyState;
    char *url;
    void *data;
    size_t bufferlen;
    size_t writepos;
    int initialized;
} ;


size_t static 
writefunction(  void *ptr, size_t size, 
                    size_t nmemb, void *userdata)
{
    size_t nbytes = size*nmemb;
        struct thread_status **this_status;
        this_status = (struct thread_status **) userdata;

        if (!(*this_status)->initialized){
                (*this_status)->data = (void *)malloc(1024);
                (*this_status)->bufferlen = 1024;
                (*this_status)->writepos = 0;
                (*this_status)->initialized = true;
        }

        if ((*this_status)->bufferlen < ((*this_status)->writepos + nbytes)){
            (*this_status)->bufferlen = (*this_status)->bufferlen + nbytes;
            (*this_status)->data = realloc((*this_status)->data, (size_t) ((*this_status)->writepos + nbytes));
        }

        assert((*this_status)->data != NULL);
        memcpy((*this_status)->data + (*this_status)->writepos, ptr, nbytes);
        (*this_status)->writepos += nbytes; 
    return nbytes;
}

void *pull_data (void *my_struct){

struct thread_status *this_struct;
this_struct = (struct thread_status *) my_struct;
this_struct->initialized = false;

cout<<(char *)this_struct->url<<"\n";

CURL *curl;
curl = curl_easy_init();
size_t rc = 0;

while(true){

    curl_easy_setopt(curl,
        CURLOPT_WRITEFUNCTION, writefunction);
    curl_easy_setopt(curl,
        CURLOPT_WRITEDATA, (void *) &this_struct);
    curl_easy_setopt(curl,
        CURLOPT_NOSIGNAL, true);
    curl_easy_setopt(curl,
        CURLOPT_URL, (char *)this_struct->url);

    if (curl_easy_perform(curl) != 0){
        cout<<"curl did not perform\n";
        exit(1);
    } else { 
    if (this_struct->data != NULL){
            // Use a binary write.
            rc = fwrite(this_struct->data, this_struct->writepos, 1, stdout);
            free(this_struct->data);
        } else {
            cout<<"Data is NULL\n";
        } 
    }

    // Tell the babysitter the thread is ready.
    this_struct->readyState = READY;
// This would pause the thread until the parent thread has processed the data in it.
//  while(this_struct->readyState == READY){;}

    // Now get ready for another round!
    this_struct->writepos = (size_t) 0;
    this_struct->initialized = false;
    this_struct->bufferlen = (size_t) 0; 

    break;
}

    curl_easy_cleanup(curl);
    return (void *)"a";
}

int main(){

    char *urls[] = { "http://www.example.com/", "http://www.google.com", "http://www.touspassagers.com/", "http://www.facebook.com/" };
    int i=0;
    struct thread_status mystatuses[4];
    for (i=0;i<4;i++){

        struct thread_status my_status; 
        char *data;

        my_status.id = i;
        my_status.readyState = NOT_READY;
        my_status.url = urls[i];
        my_status.data = data;
        my_status.bufferlen = 0;
        my_status.writepos = 0;
        my_status.initialized = false;

        mystatuses[i] = my_status;
    }

    for (i=0;i<4;i++){
        cout<<"pulling #"<<i<<"\n";
        pull_data((void *)&mystatuses[i]);
    }

}

Если кто-нибудь сможет рассказать мне об источнике моей ошибки или о том, как ее исправить, я был бы признателен.

Ответы [ 2 ]

3 голосов
/ 16 января 2011

Вы можете использовать valgrind , чтобы помочь определить источник проблем с памятью.

1 голос
/ 16 января 2011

Понял!

Очевидно, 1 КБ недостаточно памяти для обработки первого буфера cURL. Я изменил 1024 нбайт, и это работает!

Перед тем, как память, помещенная в буфер, запустит выделенную память, что приведет к повреждению.

Я написал об этом, если кому-то захочется увидеть полную реализацию: http://www.touspassagers.com/2011/01/a-working-curlopt_writefunction-function-for-libcurl/

...