Winsock2 and Threads: a bad combination?

Started by
9 comments, last by DerekSaw 16 years, 1 month ago
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
Advertisement
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?
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.
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.
Ok, thanks for the advice.

As a matter of fact, I've spent some time today searching for a decent debugger.

I'm using MinGW but am now downloading the Windows debugger..
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.
while (tired) DrinkCoffee();
Actually, they don't share any variable.. so I'm pretty sure that it's something in winsock.
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?
This is utter crap. I don't know what is wrong but it has something to do with the memory.
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement