C++ CURL 无法正确检索网页

C++ CURL not retrieving webpage properly

我的class-

中有以下三种方法
void WebCrawler::crawl()
{
    urlQueue.push("http://www.google.com/");
    if(!urlQueue.empty())
    {
        std::string url = urlQueue.front();
        urlQueue.pop();
        pastURLs.push_back(url);
        if(pastURLs.size()>4000000)
        {
            pastURLs.erase(pastURLs.begin());
        }
        std::string data=getData(url);
        auto newPair= std::pair<std::string, std::string>(url, data);
        dataQueue.push(newPair);
    }

}

std::string WebCrawler::getData(std::string URL)
{
    std::string readBuffer = "";
    CURL *curl = curl_easy_init();

    if(curl)
    {
    CURLcode res;
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WebCrawler::WiteCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
    curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    }
    return readBuffer;
}

size_t WebCrawler::WiteCallback(char* buf, size_t size, size_t nmemb, void* up)
{

    ((std::string*)up)->append((char*)buf, size * nmemb);
    return size * nmemb;
}

当我将这些方法从我的 class 和 运行 中作为函数取出时,我的代码可以正确执行并且 returns 网页内容。但是,一旦我将这些方法放入我的 class 中,它们就会开始表现不同。当我的 WriteCallback 被调用时,程序失败并表示它无法分配 45457340335435776 字节的数据。我对导致此更改的原因感到有些困惑,我们将不胜感激。

WebCrawler::WiteCallback是一个非静态方法,也就是说需要传递指向对象(this)的指针。根据 ABI,这可以是一个隐式参数,一个不用于正常参数传递的寄存器,或其他任何东西。对于您的 ABI,对象似乎作为最左边的参数传递 ("(WebCrawler *this, char* buf, size_t size, size_t nmemb, void* up)")。

你不能那样做。使 WebCrawler::WiteCallback 静态化或使用蹦床:

size_t WebCrawler::WriteCallbackTramp(char* buf, size_t size,
                                      size_t nmemb, void* up)
{
    return ((WebCrawler*) up)->WriteCallback(buf, size, nmemb);
}

其中 WebCrawler 包含缓冲区的成员。

使方法静态化是更好的解决方案。

C.f。 Wikipedia: Calling convention