CURL download_progress throws SIGSEGV

CURL download_progress throws SIGSEGV

本文关键字:throws SIGSEGV progress download CURL      更新时间:2023-10-16

我必须更新download_progress中的进度条值,但是每当我尝试更新myClass的成员函数时,它会一直发送分段的错误 这是我的代码:

class myClass{
    int percent;
    ProgressBar bar;
public:
    int download(const char* url, const char* filename, int enable_progress);
    int download_progress(void *p, double dl, double dlnow, double ul, double ulnow);
};
int myClass::download_progress(void *cli, double dl, double dlnow, double ul, double ulnow)
{
    double p = (dl / dlnow) * 100;
    myClass *mp = (myClass *)cli;
    mp->percent = (int)dl;
    mp->bar.setVal(p)
    return 0;
}
int myClass::download(const char* url, const char* filename)
{
    int res = 0;
    CURL* handle = curl_easy_init();
    if (handle == NULL) {
    return -1;
    }
    FILE* f = fopen(filename, "wb");
    if (!f) {
    return -1;
    }

    curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
    curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, NULL);
    curl_easy_setopt(handle, CURLOPT_WRITEDATA, f);
    curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, (curl_progress_callback)&myClass::download_progress);
    curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L);
    curl_easy_setopt(handle, CURLOPT_USERAGENT, "InetURL/1.0");
    curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1);
    curl_easy_setopt(handle, CURLOPT_URL, url);
    curl_easy_perform(handle);
    curl_easy_cleanup(handle);
    off_t sz = ftello(f);
    fclose(f);
    if ((sz == 0) || (sz == (off_t)-1)) {
    res = -1;
    remove(filename);
    }
    return res;
}

尝试从URL下载文件到文件名,但它一直在投掷sigsegv 为什么我无法在上述代码中更新MyClass的成员... 任何帮助将不胜感激

谢谢

首先您需要做

&myClass::download_progress

进入静态功能,并将其称为:

&download_progress

您没有发送要调用的用户数据。您的下载方法应该是这样:

curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, (curl_progress_callback)&myClass::download_progress);
curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, this);

另外,您的download_progress应该标记为静态:

class myClass{
    int percent;
    ProgressBar bar;
public:
    int download(const char* url, const char* filename, int enable_progress);
    static int download_progress(void *p, double dl, double dlnow, double ul, double ulnow);
};