Thread: Graceful Crash on Bad Pointer

  1. #1
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147

    Graceful Crash on Bad Pointer

    I've looked up a number of articles and forum posts about detecting bad pointers and other memory violations and it occured to me that a segfault (I assume that the "This program has stopped responding and needs to be closed" provided by Windows is the same thing) is enough of an indicator of a problem.

    However, to the end user should a segfault happen and the progam just hang and 'stop responding', it's irritating to have to force it closed and not have the program say anything at all about what the problem was.

    Is there a way that I could 'gracefully crash' (e.g., have the program basically say "Ooops, I'm screwed. Here's a stack trace for the dev's." rather than explode inexplicably) that I can implement using Microsoft's compiler? I know that I can do this rather simply using GCC based compilers (http://stackoverflow.com/questions/7...g-gcc-compiler) using signal hooks and plan to implement that code in the Mac/Linux versions of the program but I'm using VC++ for the Windows version.

    Is there a similar method for microsoft based compilers?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You can use signal() in the POSIX emulation layer, or you can use SEH exception handlers to catch the invalid memory reference.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I would recommend looking into smart pointers (e.g boost::shared_ptr or std::tr1::shared_ptr). Just as you would use std::vector (or other containers) for a number of dynamically allocated things and never have any troubles with dynamic arrays, you'd use smart pointers for single resources and never (or rarely) have any problems with those. Programs don't need to crash at all since memory management can be rather easy even in C++.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by anon View Post
    I would recommend looking into smart pointers (e.g boost::shared_ptr or std::tr1::shared_ptr). Just as you would use std::vector (or other containers) for a number of dynamically allocated things and never have any troubles with dynamic arrays, you'd use smart pointers for single resources and never (or rarely) have any problems with those. Programs don't need to crash at all since memory management can be rather easy even in C++.
    Smart pointers can't prevent out-of-bounds access, though. They can for the special case of NULL (throwing an exception instead of crashing), but how can they possibly know if some non-NULL pointer value is valid or not?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by leeor_net View Post
    Is there a way that I could 'gracefully crash' (e.g., have the program basically say "Ooops, I'm screwed. Here's a stack trace for the dev's." rather than explode inexplicably) that I can implement using Microsoft's compiler?
    You'll need to register an exception filter (either SetUnhandledExceptionFilter or AddVectoredExceptionHandler) then use the ContextRecord member of the parameter to start a stack walk with StackWalk64.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    31
    Quote Originally Posted by leeor_net View Post
    However, to the end user should a segfault happen and the progam just hang and 'stop responding', it's irritating to have to force it closed and not have the program say anything at all about what the problem was.
    I thought that any time a program segfaults (or in the Microsoft world, generates an access violation exception), the program is stopped by the default signal/vectored exception handler. I suppose that there may be variations, depending on the OS and how such is handled in the context of multithreading, but if the program does not crash, I don't think it's actually generating a fault.

    In Windows, when a program "hangs" (for which "program" indicates GUI programs for the most part), it's because the program has stopped processing messages. This does not mean that the program has stopped executing, but that it is still executing (but is simply not processing messages, such as when the program starts processing, but does not complete processing a message). It might not be operating as intended, but it is still running.
    Last edited by tjb; 03-03-2009 at 08:40 PM.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by tjb View Post
    I thought that any time a program segfaults (or in the Microsoft world, generates an access violation exception), the program is stopped by the default signal/vectored exception handler. I suppose that there may be variations, depending on the OS and how such is handled in the context of multithreading, but if the program does not crash, I don't think it's actually generating a fault.
    True. But a bug in some kind of "smart" crash handling code could easily cause an infinite loop. If you're going to catch crashing exceptions, you really should be doing the absolute minimal to process them, then terminate. A hung program is even worse than a crash, because the user might not think anything is wrong, or end up waiting for some operation that will never finish because everything is hosed.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    Windows reports a program as not responding for a variety of reasons. In the case of a segfault, it immediatly stops with a message "The program needs to be terminated" or something to that effect. If it's not responding because of an infinite loop, it says something like "The program appears to be busy, do you want to terminate it or wait?". So Windows knows when a program segfaults or not it just uses a lot of fluff when telling you so (e.g., for the idiots that don't know their elbow from the power button). But basically, I know when it's blowing up due to a bad pointer (always my fault usually because I did something stupid).

    Using a posix emulation layer makes no sense to me -- this is a graphical application and the code that is generated is native Win32 -- e.g., I have no reason to use an emulation layer (not to mention I haven't got a clue how to implement one). I would like to use a microsoft equivalent to the signal() hook function but my good searches haven't turned up anything.

    Quote Originally Posted by adeyblue View Post
    You'll need to register an exception filter (either SetUnhandledExceptionFilter or AddVectoredExceptionHandler) then use the ContextRecord member of the parameter to start a stack walk with StackWalk64.
    This seems like a promising lead. I'll explore this further.

    Thank you!

  9. #9

  10. #10
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    Quote Originally Posted by brewbuck View Post
    Smart pointers can't prevent out-of-bounds access, though. They can for the special case of NULL (throwing an exception instead of crashing), but how can they possibly know if some non-NULL pointer value is valid or not?
    Not only that, they can't prevent logic errors such as a division by zero (found on of those, whoops), video memory leaks (guilty on that one too) or the wrath of a third-party program (e.g., API with a flaw in it, malicious code, hardware errors, etc.).

    I've accept that as a flawed human, I write flawed code. Inevitably there are going to be bugs in any software that I write be it pointer errors, flawed logic, security holes, etc. One thing I've always hated is when a program has bugs that cause a crash and then requires me, the user, to report to the developer what happened.

    In this way, I can have the program automatically report errors to me without the user having to get involved. They know that the problem was reported and can even get a report back stating that we've acknowledged a particular problem.

    Regardless of how a crash is handled, the fact that it's handled looks a lot better than having the Operating System tell you that the program you were using blew up (e.g., the infamous "This program has performed an illegal operation").

    I found a great program called BugTrap (referred by Codeplug) that does all of the things I wanted to do and many more. Going to strip it down as there are a lot of things that I really don't need but it's a great way to catch major exceptions and 'crash gracefully'.

    Thanks for everybody's help!

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Here's a stack trace for the dev's
    You can have your program generate a core dump when it crashes (for post-mortem debugging).

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by cyberfish View Post
    You can have your program generate a core dump when it crashes (for post-mortem debugging).
    You'll need to register an exception filter (either SetUnhandledExceptionFilter or AddVectoredExceptionHandler) then use the ContextRecord member of the parameter to start a stack walk with StackWalk64.
    We all this on a large project at work. We generate a call stack, optionally log it and/or generate a mini dump and/or display it in a message box (in that order). No I can't show any of the code sorry.

    Note that you can also catch certain exceptions like access violations that you can't normally catch, using __try and __except.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    Yes, it's already dumping a stack trace for me which includes the signal that caused the error (this is on Win32). On our Mac/Linux side we're using signal() to register an exception handler that does a stack trace as well and dumps it to a file.

  14. #14
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    Am I the only one who thinks that the "This program has stopped responding and needs to be closed" is not a seg fault, but a freeze, caused by an infinite loop or some such thing?

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, I suppose it's easy to make a mistake between
    This program has stopped responding and needs to be closed
    and
    This program has stopped working and needs to be closed
    I don't think the former really exists, either.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  2. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM