0

Normally when you call assert(foo()) and the assertion fails, the program automatically aborts. Is there any way to add some sort of handler that can clean up some resources before exiting?

Matt
  • 21,026
  • 18
  • 63
  • 115

2 Answers2

3

You can handle the SIGABRT signal and do cleaning. Of course assert is just for debugging the application, so it is better to just fix the problem causing the failure instead of writing code to gracefully fail.

perreal
  • 94,503
  • 21
  • 155
  • 181
0

The whole point of assert is to dump __FILE__ and __LINE__ information and call abort() immediately. If you don't want to do that, roll your own error checking with a cleanup handler, or do as suggested in another answer and catch the abort() itself. To your specific question, assert is just a macro, so you can't hook it directly. For chapter and verse, see ISO C99, 7.2.1, p2.

Randy Howard
  • 2,165
  • 16
  • 26