Skip to main content.

Tuesday, December 12, 2006

MS-Windows is an infinite source of amusement for a twisted coder as I am. I just discovered that closing a random HANDLE may lead to funny crashes around. Wanna read about it? I generally use _beginthreadex() function to create a thread in windows; but this time, I don't know why, I had a _beginthread() around. Also, as I didn't needed the returned HANDLE, I just performed a nice CloseHandle( _beginthread(...)).

The fun thing is that I had some crash around in the program, that wasn't seemingly related to anything near. To cut the story short, I was opening a file handle, in a thread, performing the CloseHandle( _beginthread()) in another unrelated thread and then writing to the formerly opened file. At random spots, I had crashes during the file writes.

The point is that the funny _beginthread() function may NOT return a handle and silently fail in an unpredictable way, i.e. if the thread terminated before _beginthread() had the chance to return (don't ask me why, that's just how it does). To add fun to the fun, closing that "non-thread" return didn't cause an error during the CloseHandle(), and for sure not a crash. It was only during "a" subsequent I/O that a crash happened. Luckily, I remembered how much crappy WinSDK is, and I suspected that spot. Changing _beginthread() with _beginthreadex(), which is granted to return a valid handle, fixed the thing.

The day MS-Windows will be removed from any PC will be a bright day for humanity.

Comments

No comments yet

Add Comment

This item is closed, it's not possible to add new comments to it or to vote on it