Pthreads&;Libcurl-使用授权标头时出现分段错误 我在过去3天里一直在梳理我的头发,试图弄清楚为什么我的C++代码会输出一个“分割错误”。新的C++也没用。使用pthreads也无助于缩小问题的范围(如果您无法复制它,我想可以使用pthreads进行尝试)

Pthreads&;Libcurl-使用授权标头时出现分段错误 我在过去3天里一直在梳理我的头发,试图弄清楚为什么我的C++代码会输出一个“分割错误”。新的C++也没用。使用pthreads也无助于缩小问题的范围(如果您无法复制它,我想可以使用pthreads进行尝试),c++,segmentation-fault,pthreads,libcurl,C++,Segmentation Fault,Pthreads,Libcurl,但现在,我相信我终于找到了原因之一。代码如下: static void* pull_one_url(void* arguments) { struct example_struct args = *((struct example_struct *)arguments); CURL *curl; curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, ("https://example.com/" + (arg

但现在,我相信我终于找到了原因之一。代码如下:

static void* pull_one_url(void* arguments)
{

  struct example_struct args = *((struct example_struct *)arguments);

  CURL *curl;
  curl = curl_easy_init();
  curl_easy_setopt(curl, CURLOPT_URL, ("https://example.com/" + (args).appendData).c_str());
  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 30000L);

  // Set Headers
  struct curl_slist *header = NULL;
  header = curl_slist_append(header, ("Authorization: Bearer " + (args).myBearerToken).c_str());    
  header = curl_slist_append(header, "User-Agent: MyCPPApp");
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);

  CURLcode res = curl_easy_perform(curl);
  cout << "Request sent." << endl;


  return NULL;
}
静态void*拉取一个url(void*参数)
{
struct example_struct args=*((struct example_struct*)参数);
卷曲*卷曲;
curl=curl_easy_init();
curl_easy_setopt(curl,CURLOPT_URL,(“https://example.com/“+(args.appendData.c_str());
卷曲位置(卷曲,卷曲位置,1L);
curl_easy_setopt(curl,CURLOPT_TIMEOUT_MS,30000升);
//设置标题
struct curl_slist*header=NULL;
header=curl\u slist\u append(header,(“Authorization:Bearer”+(args.myBearerToken.c_str());
header=curl\u slist\u append(header,“用户代理:MyCPPApp”);
curl_easy_setopt(curl,CURLOPT_HTTPHEADER,header);
curl\u easy\u setopt(curl,CURLOPT\u WRITEFUNCTION,WriteCallback);
CURLcode res=curl\u easy\u perform(curl);

如果您在Linux上运行,请使用Valgrind跟踪sefault。或者只使用调试符号构建并将核心文件加载到gdb。要打印到控制台,您需要什么?您可以使用一个结合了条件变量的互斥锁在主线程和任何线程之间同步。