由单独源文件中的全局变量引起的 C++ 分段错误

2022-01-12 00:00:00 string segmentation-fault c++

我现在是 4 小时的 C++ 新手故障排除.我遇到了第一个分段错误.我认为它来自可变数据.该程序从网页中提取 html(使用 cURL),但在获取一些 HTML 后出现段错误.我正在调用curlbar::getThreads();"来自 main.cpp.代码在 main.cpp 中运行良好,但是当我将其放入 curlbar 时,出现 segfault (core dumped) 错误:

I'm a c++ noob trouble shooting for 4 hours now. I am getting my first segmentation fault. I think it is coming from the variable data. The program pulls the html (using cURL) from a webpage but seg faults after fetching some HTML. I am calling "curlbar::getThreads();" from main.cpp. The code worked fine when it was all in main.cpp, but when I put it into curlbar, I got the segfault (core dumped) error:

/*
* curlbar.cpp
*
*  Created on: Feb 2, 2014
* 
*/
//get list of threads
#include "headers.h"
class curlbar{
public:
string data;
size_t writeContents(char* buf, size_t size, size_t nmemb, void* up){
        for(unsigned int c = 0; c<size*nmemb; c++){
            this->data.push_back(buf[c]);
        }
        return size*nmemb;
}

static void getThreads(){

    CURL* curl;
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();

    curl_easy_setopt(curl, CURLOPT_URL, "www.somewebsiteblahblah.com");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curlbar::writeContents);
    curl_easy_setopt(curl, CURLOPT_VERBOSE,1L); //tell curl to output its progress
    curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    curl_global_cleanup();
}
};

是字符串数据;"没有足够的内存分配给它?我应该如何解决这个问题?

Is the "string data;" not have enough memory allocated to it? How should I go about fixing this?

推荐答案

我很确定这就是你所缺少的.您不能将成员函数作为回调传递,因为调用者不知道如何适当地将 this 作为第一个参数推送.

I'm fairly sure this is what you're missing. you can't pass a member function as a callback, as the caller has no clue how to appropriately push this as the first parameter.

但你可以这样做:

class curlbar
{
private:
    // callback version used for curl-write-function
    static size_t writeContents_s(char *buf, size_t size, size_t nmemb, void *up)
    {
        curlbar* pThis = static_cast<curlbar*>(up);
        return pThis->writeContents(buf, size, nmemb);
    }

public:
    std::string data;

    // member version
    size_t writeContents(char* buf, size_t size, size_t nmemb)
    {
        std::copy(buf, buf+(size*nmemb), std::back_inserter(data));
        return size*nmemb;
    }

    void getThreads()
    {
        CURL* curl;
        curl_global_init(CURL_GLOBAL_ALL);
        curl = curl_easy_init();

        curl_easy_setopt(curl, CURLOPT_URL, "www.somewebsiteblahblah.com");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curlbar::writeContents_s);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, this); // NOTE ADDITION
        curl_easy_setopt(curl, CURLOPT_VERBOSE,1L); //tell curl to output its progress
        curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        curl_global_cleanup();
    }
};

给定一个 curlbar obj; 对象,您将其调用为

Given a curlbar obj; object, you invoke it as

curlbar obj;
obj.getThreads();

工作原理

这使用easy-curl句柄的用户定义数据参数选项来建立将与您的static writer-callback一起发回的piece-o-data.该数据作为回调的 void *up 参数提供.所以我们传递指针 this 并在静态回调中使用 static_cast 给我们一个对象指针 pThis.我们使用该指针来触发类似名称的 member 函数,但不再需要 up 参数.

This uses the user-defined data parameter option of an easy-curl handle to establish the piece-o-data that will be send back along with your static writer-callback. That data is provided as the void *up parameter to the callback. So we pass the pointer this and in the static callback use a static_cast to give us an object pointer, pThis. We use that pointer to fire the member function of a similar name, but no longer needing the up param.

相关文章