mingw program crashes when throwing exception inside secondary thread

This is a discussion on mingw program crashes when throwing exception inside secondary thread within the C++ Programming forums, part of the General Programming Boards category; I have a windows service that I am working on, and it crashes any time I throw any kind of ...

mingw program crashes when throwing exception inside secondary thread

I have a windows service that I am working on, and it crashes any time I throw any kind of exception from a secondary thread (not the main thread). I googled for at least a half hour looking for the answer to this, but found nothing. I am compiling with the -mthreads command line option, which is supposed to help, but it makes no difference. I have now spent about 3 hours on this problem alone, and I absolutely cannot move on until it is resolved, as the remainder of my program will use exceptions, and must not crash.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

without trying to sound like an ass, there's no specific code that does it.

from my original post:

Originally Posted by elkvis

it crashes any time I throw any kind of exception from a secondary thread

that sums it up pretty well.

Originally Posted by brewbuck

For that matter what exactly do you mean by "service," an actual Windows service or just some kind of server program?

it is an actual service that starts the service control dispatcher, etc.

at any given time, there is a minimum of 3 threads running.

1. the main thread
2. the named pipe communication thread (also spawns additional threads for connections to the pipe)
3. the worker thread

it is inside the worker thread where I first noticed the problem. I originally noticed that when I tried to load a config file with my config file class, which throws exceptions upon failing to perform operations related to the config file, if there was a problem with the file, it would just crash instead of catching the exception. I then did more testing to see if other types of exceptions (other than my ConfigException type) would do it, and it turns out that any exception will crash the program, even if I just throw an int. I'm really confused, and running out of things to try.

So where exactly is this crash happening? Yes, it always happens when you throw an exception, but where exactly in the code does the crash happen? Inside your code or Windows code?

I log data to a file to indicate line numbers, source file, etc, and the last line that appears in the file is the one just before the throw, and no line is inserted from inside the catch block. I don't even know if any windows code is being executed after the throw. my debugger doesn't want to work for me lately, so I'm really limited in what I can tell you.

so anyway, now that I'm back at work, I played with it a bit more and found that it only crashes when it is run as a service. the program can also be run in taskbar-icon mode, and when I do that, it does not crash upon throwing exceptions.

so now I have two new questions:
1. is it possible that running code within the windows SCM is not exception-safe?
2. would switching to microsoft's compiler help?

so anyway, now that I'm back at work, I played with it a bit more and found that it only crashes when it is run as a service. the program can also be run in taskbar-icon mode, and when I do that, it does not crash upon throwing exceptions.

so now I have two new questions:
1. is it possible that running code within the windows SCM is not exception-safe?
2. would switching to microsoft's compiler help?

Dude.. The first thing I asked was whether this is running as a service.

mingw has problems generating code that runs in service context. Switching to MS's compiler WILL help. Could have saved yourself a few days...

I suspect that these errors are a result of other libraries that I'm including, which were not compiled with this version of GCC. there doesn't seem to be a way to get around this, so I don't really know what I'm going to do about it, as the microsoft compiler is going to be a hard pill to swallow for the other developers here in my office.