【问题标题】:c++ segment fault at the end of execution执行结束时的c ++段错误
【发布时间】:2014-10-22 07:22:41
【问题描述】:

我的代码可以编译,并且大部分都按照预期进行,但在执行结束时出现段错误,假设更新(附加)文件但没有

#include <stdio.h>
#include <curl/curl.h>


size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
        size_t written;
        written = fwrite(ptr, size, nmemb, stream);
        return written;
}



int main(void)
{
        CURL *curl;
        CURLcode res;

        curl = curl_easy_init();
        if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=155");

        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);

        /* Check for errors */
        if(res != CURLE_OK)
                fprintf(stderr, "curl_easy_perform() failed: %s\n",
                        curl_easy_strerror(res));

        FILE * pFile;
        pFile = fopen ("myfile.txt","a+");
        if (pFile!=NULL)
        {
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, pFile);
                res = curl_easy_perform(curl);
                fclose (pFile);
        }

        /* always cleanup */
        curl_easy_cleanup(curl);
  }
  return 0;
}

这是 gdb 调试输出:

gdb /home/coinz/cryptsy/getprice.o /home/coinz/cryptsy/core
GNU gdb (Gentoo 7.8 vanilla) 7.8
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://bugs.gentoo.org/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/coinz/cryptsy/getprice.o...done.

warning: exec file is newer than core file.
[New LWP 665]

warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Core was generated by `./getprice.o'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000011e12a0 in ?? ()
(gdb) bt full
#0  0x00000000011e12a0 in ?? ()
No symbol table info available.
#1  0x00007f9e78b9ca48 in ?? () from /usr/lib64/libcurl.so.4
No symbol table info available.
#2  0x00007f9e78bb4cd0 in ?? () from /usr/lib64/libcurl.so.4
No symbol table info available.
#3  0x00007f9e78bb0a4a in ?? () from /usr/lib64/libcurl.so.4
No symbol table info available.
#4  0x00007f9e78bb8fe8 in ?? () from /usr/lib64/libcurl.so.4
No symbol table info available.
#5  0x00007f9e78bb9e15 in curl_multi_perform () from /usr/lib64/libcurl.so.4
No symbol table info available.
#6  0x00007f9e78bb22d6 in curl_easy_perform () from /usr/lib64/libcurl.so.4
No symbol table info available.
#7  0x0000000000400a76 in main () at getprice.cpp:35
        pFile = 0x11e12a0
        curl = 0x11ce2c0
        res = CURLE_OK
(gdb)

【问题讨论】:

  • 您是否尝试过在调试器中运行您的代码以查看它实际停止的位置?
  • 那么它可能在加载 pFile 时遇到问题,因为我没有看到定义的路径,只有一个名称字符串。
  • 程序获取网页,将所有内容输出到缓冲区,但最后一个字符是“段错误”,并且文件正确填充了数据。但是,在第二次执行时,我希望文件大小翻倍,但大小相同
  • ....,{"price":"0.00493171","quantity":"30.41540139","total":"0.14999994"},{"price":"0.00493000","quantity ":分段错误
  • gdb 输出 -> paste.ee/p/9zch4

标签: c++ curl libcurl


【解决方案1】:

下面一行是错误的:

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, pFile);

CURLOPT_WRITEFUNCTION 选项需要一个函数指针,但您传递了文件处理程序。此外,您永远不会告诉 libcurl 使用您的 write_data 函数。

您应该同时设置了写入函数和写入数据选项。

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, pFile);

更多细节可以在这里找到:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-20
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    相关资源
    最近更新 更多