curl / Mailing Lists / curl-library / Single Mail

curl-library

Re: QT | C++ | SIGSEGV | Segmentation fault failure when the code reaches the URL definition

From: Chris Carlmar via curl-library <curl-library_at_cool.haxx.se>
Date: Mon, 18 Feb 2019 13:23:12 +0100

On 18.02.2019 13:11, Sven-Meikel Auer via curl-library wrote:
> Hello to all of you,
>
> I'm new here and have my first problem where I can't get any further.
>
> My problem is that when the following code is executed the application
> terminates with "SIGSEGV | Segmentation fault".
>
> The failure is on this line - says the debugger:
> curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com");
>
> What am I doing wrong?
>
> A nice week. I wish you all the best.
> Greetings from
> Sven
>
> Here my code that I have found on the internet - but It dosen't work:
> #include "mainwindow.h"
> #include "ui_mainwindow.h"
> #include <QDebug>
> #include <curl/curl.h>
> #include <string>
> #include <sstream>
>
> MainWindow::MainWindow(QWidget *parent) :
>     QMainWindow(parent),
>     ui(new Ui::MainWindow)
> {
>     ui->setupUi(this);
> }
>
> MainWindow::~MainWindow()
> {
>     delete ui;
> }
>
> size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) {
>     std::string data((const char*) ptr, (size_t) size * nmemb);
>     *((std::stringstream*) stream) << data << std::endl;
>     return size * nmemb;
> }
>
>
> void MainWindow::on_pushButton_clicked()
> {
>             // CURL IN ACTION
>             CURL *curl;
>
>             curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com");
>
>             std::stringstream out;
>             out.clear();
>
>             curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
>             curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out);
>
>                 /* Perform the request, res will get the return code */
>                 CURLcode res = curl_easy_perform(curl);
>                 /* Check for errors */
>                 if (res != CURLE_OK) {
>                     fprintf(stderr, "curl_easy_perform() failed: %s\n",
>                             curl_easy_strerror(res));
>                 }
>
>                 // Now I can make further processing with:
>                 // out.str();
>
>                 curl_easy_cleanup(curl);
>                 curl_global_cleanup();
>
> }
>

Hi.
You are missing the curl_easy_init() (and the curl_global_init(), but
easy_init will do that if you don't).

Take a look at this simple example:
https://curl.haxx.se/libcurl/c/getinmemory.html

--
mvh
Chris Carlmar
-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette:   https://curl.haxx.se/mail/etiquette.html
Received on 2019-02-18