Error: segmentation fault

Aug 2, 2009 at 11:06pm
Hello people, I'm new to this community, but I'm also new to C++. I already know some programming languages so learning the basics of C++ was pretty easy, but I'm having a hard time figuring out why I'm getting a segmentation fault error.
I'm trying to make a libcurl wrapper to perform GET/POST HTTP requests easily... here's my code so far:

main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "test.h"
#include <curl/curl.h>

using namespace std;

int main(int argc, char* argv[])
{
    /*CURL *curl = curl_easy_init();
    CURLcode res;
    curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com/");
    curl_easy_perform(curl);*/

    rickysWrapper* w = new rickysWrapper();
    cout << "worked" << endl;
    w->getRequest("http://www.google.com/\0");
    cout << "requested" << endl;
    delete(w);
    return 0;
}


test.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <curl/curl.h>
#include <curl/easy.h>

class rickysWrapper
{
    public:
        rickysWrapper();
        ~rickysWrapper();
        std::string getRequest(char* url);
    protected:
    private:
        CURL *curl;

};


test.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "test.h"

#define DEBUG_OUTPUT true

rickysWrapper::rickysWrapper()
{
    //ctor
    curl_global_init(CURL_GLOBAL_ALL);
    this->curl = curl_easy_init();
    curl_easy_setopt(this->curl, CURLOPT_NOPROGRESS, 1);
    curl_easy_setopt(this->curl, CURLOPT_AUTOREFERER, 1);
    curl_easy_setopt(this->curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_easy_setopt(this->curl, CURLOPT_ENCODING, "");
    curl_easy_setopt(this->curl, CURLOPT_NOSIGNAL, 1);
    if (DEBUG_OUTPUT)
        std::cout << "Debug: class initialized" << std::endl;
}

std::string rickysWrapper::getRequest(char* url)
{
    std::cout << "Started function" << std::endl;
    CURLcode result;
    std::cout << "Created result object" << std::endl;
    curl_easy_setopt(this->curl, CURLOPT_HTTPGET, 1);
    //std::cout << "Set HTTPGET" << std::endl;
    curl_easy_setopt(this->curl, CURLOPT_URL, url);
    std::cout << "Set url" << std::endl;
    result = curl_easy_perform(this->curl);
    std::cout << "processed!" << std::endl;

    std::cout << curl_easy_strerror(result) << std::endl;
}

rickysWrapper::~rickysWrapper()
{
    //dtor
    std::cout << "destroying" << std::endl;
    curl_easy_cleanup(this->curl);
    curl_global_cleanup();
    if (DEBUG_OUTPUT)
        std::cout << "Debug: class destroyed" << std::endl;
}


This code compiles just fine, it even runs as it should (it gets the page and outputs it to stdout) but as soon as the function rickysWrapper::getRequest ends, it throws a Segmentation Fault error.
Anyone who can help me, please?
Aug 3, 2009 at 9:22pm
bump. Anyone who could help me?
Aug 4, 2009 at 3:04am
When you say "as soon as the function ends", where exactly is it failing? Between what lines and why do you think that?

Have you run it in a debugger or generated a core file to find out where it is failing? What OS are you running on?
Aug 4, 2009 at 10:45pm
I don't see any problems with the code that would cause a segmentation fault but I don't know anything about this curl library that you are using. Perhaps this is more of a question for the curl message boards. Have you studied those APIs to ensure that you are using them correctly? Have you stepped into the code with a debugger, line, by line so that you can see specifically which line of code crashes?
Topic archived. No new replies allowed.