在构造函数中使用curl_easy_init会导致段错误

时间:2014-09-30 06:39:01

标签: c++ libcurl

当我在课堂上将libcurl包装成问题:

Foo::Foo()
{
    curl = curl_easy_init();
    if (!curl)
        throw std::runtime("Can't initialize libcurl");
}

char* Foo::GetPage(char *url)
{
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
    curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, WriteData );
    curl_easy_setopt( curl, CURLOPT_WRITEDATA, (void *)data);

    CURLcode res = curl_easy_perform( curl );
}

我在GetPage函数的第一行(设置url)上遇到了段错误。如果我在GetPage中初始化卷曲 - 一切都好。其他人遇到过类似的问题吗?

1 个答案:

答案 0 :(得分:1)

变量正在失去它的范围。在进入GetPage函数时检查curl变量的值。

相关问题