wxSocketClient is killing me!

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
alaricljs
Earned a small fee
Earned a small fee
Posts: 10
Joined: Wed Jun 15, 2005 5:44 pm

wxSocketClient is killing me!

Post by alaricljs » Wed Jun 22, 2005 5:37 pm

Ok, I'm trying to open a socket connection and it's segfaulting. I have no idea why, there's no real indicators in the backtrace, I'm at a total loss. I think I have everything implemented that's necessary.

Below, wxString host is passed from a modal dialog with a wxTextCtrl as:
if (UI->DoConnect(text_address->GetValue())) {
The wxMessageBox does indeed show the right address, yay.
connection->Connect is the call that dies with a segfault. gdb doesn't drill into the Connect call, so it's got to be that connection doesn't exist (or something like that)

Code: Select all

bool UI::DoConnect(wxString host)
{
    wxIPV4address addr;
     
    addr.Hostname(host);
    addr.Service(4242);
     
    //(void)wxMessageBox((addr.Hostname()).c_str(), _T("test"), wxICON_INFORMATION);
     
    connection->Connect(addr, false);
    connection->WaitOnConnect(10);
    
    if (connection->IsConnected())
        return true;
    else
        return false;
}
This is the UI ctor section pertaining to the connection:

Code: Select all

    // Create the socket
    connection = new wxSocketClient();

    // Setup the event handler and subscribe to most events
    connection->SetEventHandler(*this, Socket_ID);
    connection->SetNotify(wxSOCKET_CONNECTION_FLAG |
                          wxSOCKET_INPUT_FLAG |
                          wxSOCKET_LOST_FLAG);
    connection->Notify(true);
OnSocketEvent is a stub that does nothing with the events.

So why is connection->Connect() dying ??!?
Dev-C++ 4.9.9.2
wxWidgets 2.6.1
wxGlade 0.3.5.1
WinXP SP2 (+all)

lowjoel
Moderator
Moderator
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

Post by lowjoel » Wed Jun 22, 2005 11:54 pm

what address did you try to put into the connect function?

GianT
Earned some good credits
Earned some good credits
Posts: 124
Joined: Wed Mar 16, 2005 5:44 pm
Location: Guadeloupe, French West Indies
Contact:

Post by GianT » Thu Jun 23, 2005 4:48 am

Maybe it's your host variable which causes this problem. Try passing the address yourself, like for localhost:

Code: Select all

addr.Hostname("127.0.0.1");
It's not really a wxString, but this works in my program.

alaricljs
Earned a small fee
Earned a small fee
Posts: 10
Joined: Wed Jun 15, 2005 5:44 pm

Post by alaricljs » Thu Jun 23, 2005 2:30 pm

Ok, the issue actually has nothing to do with the address. I've done the whole breakpoint/watch variable on my address section, it's picking up the address just fine.

It also has nothing to do with the ->Connect call, technically speaking. The issue is that in the UI::DoConnect() function any reference to connection segfaults because connection is apparently either not created or not pointing to the right place.

I have no idea why it would not exist (I create it in the ctor of UI:: ) and I see no reason for it to be pointing off into space.

Here is a link to a complete Dev-cpp project that is purely a demonstration of this issue. Please take a look.
Dev-C++ 4.9.9.2
wxWidgets 2.6.1
wxGlade 0.3.5.1
WinXP SP2 (+all)

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE » Thu Jun 23, 2005 3:02 pm

Easy one :D

The problem is that "top" never is set to a correct value, thus you do call DoConnect() on some test instance, but not on the one you wanted...

Add a line like:

Code: Select all

top = (test*)parent;
as the last of ConnectDialog::ConnectDialog(wxWindow* parent) and it works charming.

BTW: I'd do it another way, but it's only opinion. Why not let the main frame call the dialog and test if it returned OK, then get the string from the dialog and connect?
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

alaricljs
Earned a small fee
Earned a small fee
Posts: 10
Joined: Wed Jun 15, 2005 5:44 pm

Post by alaricljs » Thu Jun 23, 2005 3:10 pm

How very strange... I remember writing that line. And yet it's not there anymore... Hrm, probably some stupid Dev-cpp/wxGlade interaction where I didn't save my files before doing some UI changes in wxGlade...

Thanks for noticing, and the suggestions. I tried having the main frame call the connection dialog, but never figured out how to get it to work. Don't remember what I tried, but no UI ever got displayed the way I did.

Thanks again!
Dev-C++ 4.9.9.2
wxWidgets 2.6.1
wxGlade 0.3.5.1
WinXP SP2 (+all)

alaricljs
Earned a small fee
Earned a small fee
Posts: 10
Joined: Wed Jun 15, 2005 5:44 pm

Post by alaricljs » Thu Jun 23, 2005 3:20 pm

Ok, it works wonderfully in my little demo project. I go back to the big project and implement the changes.... and I get a linker error! undefined reference to vtable ??

Any idea what this means? (If you haven't noticed I've never coded in C++ before ;) )
Dev-C++ 4.9.9.2
wxWidgets 2.6.1
wxGlade 0.3.5.1
WinXP SP2 (+all)

alaricljs
Earned a small fee
Earned a small fee
Posts: 10
Joined: Wed Jun 15, 2005 5:44 pm

Post by alaricljs » Thu Jun 23, 2005 3:35 pm

Ok, my own editing stupidity but that's an awful weird error for what I did. I had a dtor defined in the class, and had no dtor function code. Yay.
Dev-C++ 4.9.9.2
wxWidgets 2.6.1
wxGlade 0.3.5.1
WinXP SP2 (+all)

Post Reply