c++ - 多线程curl应用存在内存分配问题

标签 c++ c curl libcurl

我正在开发一个 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]);
    }

}

如果有人能告诉我错误的根源或补救措施,我将不胜感激。

最佳答案

您可以考虑使用 valgrind帮助定位内存问题的根源。

关于c++ - 多线程curl应用存在内存分配问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4706586/

相关文章:

C++ 不识别 .h 声明

c - 在屏幕底部保持提示消息应用程序

c++ - 将大型核心文件转换为 "minicore"文件

c - 如何从头开始使用 C 编写 Web 浏览器

尝试下载文件时,Python 请求抛出 Connection Broken : ChunkedEncodingError with http. client.IncompleteRead

apache - curl 命令将 gzip 压缩的 POST 正文发送到 apache 服务器

c++ - 指定具有特征的模板化类

c++ - 将结构数组传递给 OpenGL

apache - curl 和 wget 在托管服务器上返回 404,在外部工作正常

c++ - Iaccessible接口(interface)的accLoacation()方法中的参数如何初始化?