执行结束时的c ++段错误

时间:2014-10-22 07:22:42

标签: c++ curl libcurl

我的代码编译并且在大多数情况下执行它的目的但是我在执行结束时遇到段错误并且假设更新(追加)文件但是没有

#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)

1 个答案:

答案 0 :(得分: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);

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