curl / Mailing Lists / curl-library / Single Mail

curl-library

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

From: Sven-Meikel Auer via curl-library <curl-library_at_cool.haxx.se>
Date: Mon, 18 Feb 2019 13:11:18 +0100

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();

}

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