Thread: mingw program crashes when throwing exception inside secondary thread

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445

    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.

    Edit:
    GCC 3.4.5
    windows threading API functions _beginthreadex, etc
    windows XP Pro x64
    Last edited by Elkvis; 08-06-2009 at 02:48 PM. Reason: more information

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Can you post the code that is causing the crash?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Does the crash occur when you run your code outside of the Windows service host? For that matter what exactly do you mean by "service," an actual Windows service or just some kind of server program?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    What mingw build are you using?

    I would try the latest SJLJ build from TDM: TDM's GCC/mingw32 Builds

    gg

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Codeplug
    I would try the latest SJLJ build from TDM: TDM's GCC/mingw32 Builds
    Incidentally, I became aware less than an hour ago that the official MinGW port of GCC 4.4.0 was released last month, so it might be better to use that if GCC 4.4 is desired.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    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.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by bithub View Post
    Can you post the code that is causing the crash?
    without trying to sound like an ass, there's no specific code that does it.

    from my original post:
    Quote Originally Posted by elkvis
    it crashes any time I throw any kind of exception from a secondary thread
    that sums it up pretty well.

    Quote 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.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    upon further testing, I find that throwing an exception from ANYWHERE in my program causes it to crash. even in the main() function before anything else happens. now I'm REALLY confused.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elkvis View Post
    upon further testing, I find that throwing an exception from ANYWHERE in my program causes it to crash. even in the main() function before anything else happens. now I'm REALLY confused.
    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?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by brewbuck View Post
    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.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    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?

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elkvis View Post
    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...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12

  13. #13
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by brewbuck View Post
    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...
    He answered your question.
    >> it is an actual service that starts the service control dispatcher, etc.
    bit∙hub [bit-huhb] n. A source and destination for information.

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by brewbuck View Post
    Dude.. The first thing I asked was whether this is running as a service.
    and I told you at that time that it is. I didn't mention the taskbar mode because at the time I didn't think it was relevant. the program crashes only when it runs in service mode.

    mingw has problems generating code that runs in service context. Switching to MS's compiler WILL help. Could have saved yourself a few days...
    are there any ways to work around this? I tried downloading GCC 4.4.0 for mingw, but i got a lot of linker errors:

    'undefined reference to _Unwind_SjLj_Register'
    'undefined reference to _Unwind_SjLj_Unregister'
    'undefined reference to _Unwind_SjLj_Resume'

    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.

  15. #15
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> which were not compiled with this version of GCC
    Yeah, any static libs should be recompiled with the same version... You can't recompile them?

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 11:34 PM
  2. Thread inside a thread ?
    By lsme in forum Linux Programming
    Replies: 3
    Last Post: 12-08-2004, 11:08 PM
  3. Comment this Program
    By kishorepalle in forum C Programming
    Replies: 11
    Last Post: 10-05-2004, 06:41 AM
  4. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  5. How to make a thread sleep or std::recv timeout?
    By BrianK in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2003, 10:27 PM