SegFault on GetInputStream

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

SegFault on GetInputStream

Post by FlyingIsFun1217 » Wed Nov 23, 2011 5:02 am

Greetings,

After many years of inactivity, I've decided to try my hand at programming again.

To the point: I've been working on a simple HTTP GET request to youtube, attempting to get an XML document through their fairly simple API. When I go to use wxHTTP::GetInputStream though, I get (with certain regularity) a Segmentation fault.

Code below:

Code: Select all

wxString wxGETRequest(wxString pageDir)
{
    wxString response = wxEmptyString;

    wxHTTP request;
    request.Connect(wxT("gdata.youtube.com"));
    wxInputStream *serverStream = request.GetInputStream(pageDir);

    if(request.GetError() == wxPROTO_NOERR)
    {
        wxStringOutputStream outputStream(&response);
        serverStream -> Read(outputStream);
    }

    else response = wxEmptyString;

    wxDELETE(serverStream);
    request.Close();

    return response;
}

int main()
{
    string      Query;
    wxString    wxQuery;
    wxString    wxQueryURI;

    cout << "Enter search term: ";

    getline(cin, Query);

    wxQuery = wxQuery.FromAscii(Query.c_str());
    wxQuery.Replace(wxT(" "), wxT("+"), true);

    wxQueryURI = wxT("/feeds/api/videos?q=") + wxQuery + wxT("&max-results=10&v=2");

    cout << endl << wxQueryURI.ToAscii() << endl << endl;

    wxString searchResponseString = wxGETRequest(wxQueryURI);

    cout << endl << searchResponseString << endl;
}
Is this normal behavior? Something to do with not having any type of actual GUI in the program?

Program output:
Enter search term: test

/feeds/api/videos?q=test&max-results=10&v=2

Segmentation fault
Thanks for the help!
FlyingIsFun1217

User avatar
doublemax
Moderator
Moderator
Posts: 14098
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: SegFault on GetInputStream

Post by doublemax » Wed Nov 23, 2011 9:11 am

If you use wxWidgets from main() without an wxApp object, you need to initialize wxWidgets first. You can do this by wrapping wxInitialize()/wxUninitialize() calls around your code, or by creating an instance of wxInitializer (which does the same internally).
http://docs.wxwidgets.org/stable/wx_app ... initialize

Are you sure that the call to GetInputStream() crashes? I would have expected that GetInputStream() returns NULL (for whatever reason) and that the following code crashes, because you didn't check the pointer for NULL.
Use the source, Luke!

FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

Re: SegFault on GetInputStream

Post by FlyingIsFun1217 » Wed Nov 23, 2011 4:45 pm

Yep, that did the trick, thanks!

Also, I had pinpointed it down to that one line by commenting everything else out in the wxGETRequest function.

FlyingIsFun1217

Post Reply