wxSocketClient and wxThread - if timeout is above 3 secs app crashes

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
d2uriel
In need of some credit
In need of some credit
Posts: 2
Joined: Sun Jan 23, 2022 8:09 pm

wxSocketClient and wxThread - if timeout is above 3 secs app crashes

Post by d2uriel »

Hi everyone,

I'm struggling with a problem for a while now trying to find out what am I doing wrong but I can figure it out. I'm a newbie to wxWidgets and multi-threading so please forgive me. I did read the pinned thread about Sockets but I couldn't get any closer to a solution. First of all:
IDE: Code::Blocks 17.12
OS: Debian 10 (on a virtual machine)
wxWidgets: 3.0.4.0

I have a class derived from wxThread:

Code: Select all

class myThread: public wxThread {
	public:
		myThread(myClass *src, myClass *dst);
		virtual ~myThread();
		
		protected:
			virtual wxThread::ExitCode Entry();
			
		private:
			myClass *_src;
			myClass *_dst;
			wxSocketClient *_c;
};
Implementation:

Code: Select all

myThread::myThread(myClass *src, myClass *dst): wxThread(wxTHREAD_DETACHED) {
	_src = src;
	_dst = dst;
	_c = nullptr;
}

myThread::~myThread() {
	_dst = nullptr;
	_src = nullptr;
	_c = nullptr;
}

wxThread::ExitCode myThread::Entry() {
	_c = new wxSocketClilent();
	_c->SetTimeout(3); // if this will be greater than 3 app will crash
	wxIPV4address ip;
	ip.Hostname(_dst->GetIp());
	ip.Service(_dst->GetPort());
	if(_c->Connect(ip)) {
		// do something
	} else {
		// failed to connect
	}
	_c->Destroy();
	return (wxThread::ExitCode)0;
}
I create myThread this way from a method in my main GUI:

Code: Select all

myThread *t = new myThread(src, dst); // src and dst are NOT null pointers ;-)
if(t->Run() != wxTHREAD_NO_ERROR) {
	// we have an error
}
I simplified the code to this - I get the exact same problem with such minimalistic code. Sorry for any typos there - I had to write it by hand, clipboard sharing between host and guest OSes got messed up and is not working. Hopefully you get the idea.

Anyways, if I set the timeout for wxSocketClient to anything above 3 seconds than the app will crash after the specified time. When I setup a breakpoint on the _c->Connect(ip) line then I see this in the debug window as soon as this piece of code is ran:

Code: Select all

In __kernel_vsyscall () ()
	../sysdeps/unix/sysv/linux/internal-signals.h: No such file or directory.
	0xf6eb4382 in __libc_signal_restore_set (set=0xf51ac68c) at ../sysdeps/unix/sysv/linux/internal-signals.h:84
At the very end, when I try to go to the next line of code, I get:

Code: Select all

Program terminated with signal SIGABRT, Aborted.
What am I doing wrong here?

Thank you in advance for your help.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxSocketClient and wxThread - if timeout is above 3 secs app crashes

Post by doublemax »

This is not much to go by...

Did you call wxSocketBase::Initialize() from the main thread?
https://docs.wxwidgets.org/trunk/classw ... 0f3d17b29f

Did you build and run the "sockets" sample that comes with wxWidgets?

You should also read the "sockets" chapter in the wxWidgets book. Although the book is pretty old, its socket chapter is still relevant and very good. (I can't post a link here, but if you google for it, you should be able to find a PDF).
Use the source, Luke!
d2uriel
In need of some credit
In need of some credit
Posts: 2
Joined: Sun Jan 23, 2022 8:09 pm

Re: wxSocketClient and wxThread - if timeout is above 3 secs app crashes

Post by d2uriel »

Hey doublemax,

Yes, I did call wxSocketBase::Initialize() in the main GUI thread.
I did not look at the sockets example - but now I did. Thank you. I figured out what the problem was.

This piece of code:

Code: Select all

myThread *t = new myThread(src, dst); // src and dst are NOT null pointers ;-)
if(t->Run() != wxTHREAD_NO_ERROR) {
	// we have an error
}
Should look like this:

Code: Select all

myThread *t = new myThread(src, dst); // src and dst are NOT null pointers ;-)
if(t->Create() != wxTHREAD_NO_ERROR) {
	if(t->Run() != wxTHREAD_NO_ERROR) {
		// we have an error
	}
}
I did not call Create(). This seems to be the problem because now it works fine I think. Thank you once again.

Cheers.

PS: I failed to add this reply earlier, sorry for that. Hopefully this will help a newbie like me.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxSocketClient and wxThread - if timeout is above 3 secs app crashes

Post by PB »

d2uriel wrote: Fri Feb 18, 2022 11:06 am I did not call Create(). This seems to be the problem because now it works fine I think.
This is odd since the documentation Note for wxThread::Create() says
It is not necessary to call this method since 2.9.5, Run() will create the thread internally. You only need to call Create() if you need to do something with the thread (e.g. pass its ID to an external library) before it starts.
Post Reply