gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:27 25 4
gpt4 key购买 nike

我正在开发一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com