简体   繁体   English

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

[英]C++ Segmentation Fault caused by global variable in separate source file

I'm a c++ noob trouble shooting for 4 hours now. 我现在是4个小时的c ++ noob麻烦。 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. 该程序从网页中提取html(使用cURL),但在获取一些HTML后会出现seg错误。 I am calling "curlbar::getThreads();" 我叫“curlbar :: getThreads();” from main.cpp. 来自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: 当它全部在main.cpp中时,代码运行正常,但是当我把它放入curlbar时,我得到了段错(核心转储)错误:

/*
* 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? 我该怎么办呢?

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. 你不能将成员函数作为回调传递,因为调用者不知道如何将this作为第一个参数适当地推送。

But you can do this: 但你可以这样做:

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();
    }
};

Given a curlbar obj; 给出一个curlbar obj; object, you invoke it as 对象,你调用它

curlbar obj;
obj.getThreads();

How It Works 这个怎么运作

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. 这使用easy-curl句柄的用户定义数据参数选项来建立将与静态 writer-callback一起发回的piece-o-data。 That data is provided as the void *up parameter to the callback. 该数据作为回调的void *up参数提供。 So we pass the pointer this and in the static callback use a static_cast to give us an object pointer, pThis . 所以我们传递指针this并在静态回调中使用static_cast给我们一个对象指针pThis We use that pointer to fire the member function of a similar name, but no longer needing the up param. 我们使用该指针来触发类似名称的成员函数,但不再需要up参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM