Sign in to follow this  

Winsock2 and Threads: a bad combination?

This topic is 3573 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've been working on a win32 application that starts 2 threads, which both connect to the same FTP-server and do various downloads/uploads/renames/deletes etc. at the same time. The main function kind of works like this:
	//Initialise winsocket
	FTP::init();

	//Start synchronisatie thread
	synchThread = CreateThread(NULL, 0, runSynchronisation, NULL, 0, 0);

	//Start order update thread
	orderThread = CreateThread(NULL, 0, runOrders, NULL, 0, 0);

	while(true);
The problem is that after a short while (about a minute) the application crashes. But if I make two seperate applications, each one just having one of the two threads, it will run for hours without a problem. Personally, I don't see any reason why this should happen, as the threads do not share any variables (maybe only in winsock, but this is the concern of winsock, right?) Does anyone have any idea what might be the problem here? Cheers, Bas

Share this post


Link to post
Share on other sites
There's nothing wrong with using winsock in threads, in fact it's the usual way to do things.

What do you mean the application crashes? What does the debugger tell you is wrong? You are running it in the debugger rather than just running blind, aren't you?

Share this post


Link to post
Share on other sites
It seems to crash on random points and with random codes.

I've seen C000001D, C0000096 and C0000005.

And btw, I don't know a good win32 debugger.. Actually, I haven't got much experience in win32 at all.

Share this post


Link to post
Share on other sites
Visual Studio Express 2008 is free and comes with an excellent debugger. It's practically impossible to write any code without a debugger. Well, I suppose it's possible, but it's an extremely bad idea and will take an extremely long time to track even the simplest bugs down.

Share this post


Link to post
Share on other sites
This might be a stupid question, but are you making sure your threads are not trying to write to the same memory locations at once? Since it crashes randomly, this could be the cause.

Share this post


Link to post
Share on other sites
Ok I've found out where the problem occurs.

Most of the time, it occurs at the instant of 'throwing' a user-defined Exception. The Exception is created correctly, but when it is thrown the control never reaches the catch-statement, and instead the thread crashes (dialog pops up).

Is there any way it coule be related to the thread heap size? And how can I check for overflow?

Share this post


Link to post
Share on other sites
Please post details of your crash situation. What is the exact error message when the app dies? What is the current state of the program stack when it dies? Why is the exception being thrown and what does the code around that context look like? How is the exception class implemented? How do you know you don't have any shared memory issues?

You might try downloading and running Intel's Thread Checker to see if it detects any problems. It's an excellent tool, but be warned, it can take a long time to run.

Share this post


Link to post
Share on other sites
Make sure each of your 'thread' functions are wrapped with try/catch. Exception can't get out from its own thread and being catched in other threads.

If your individual threads aren't catching any exception at all, throwing anything within the thread will just exit that thread.

Share this post


Link to post
Share on other sites

This topic is 3573 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this