Stack overflows not caught by madExcept?

delphi package - automated exception handling
Post Reply
zunzster
Posts: 58
Joined: Wed Oct 29, 2008 3:43 am

Stack overflows not caught by madExcept?

Post by zunzster »

I'm using madExcept 4.0.17 and Delphi 10.1 Berlin. We have a subscription to madExcept 5.0 but I've not gotten around to switching over our main application to use it. My bad.

Based on a report from the field, I wrote a simple single form test application that causes a stack overflow due to recursion. I just added a button which has an OnClick handler which calls itself.
When I click the button, after a pause the app just disappears without any error indication. I'm pretty sure earlier Delphi versions would give you a runtime error 202 or an EStackOverflow message but apparently not any more, which is rather unhelpful.

However, If I enable madExcept and try again, the app still just disappears. Is this to be expected? Can madExcept catch these? Is there a setting I'm missing?
Last edited by zunzster on Sun Nov 03, 2019 9:27 pm, edited 1 time in total.
zunzster
Posts: 58
Joined: Wed Oct 29, 2008 3:43 am

Re: Stack overflows not caught by madExcept?

Post by zunzster »

After a bit of debugging, I've determined that madExcept is getting the stack overflow exception and marshaling it to the madExcept thread but then Windows dies whilst trying to pump the message loop that waits for the dialog in the madExcept thread.

If I turn off the undocumented HandleMessagesInMainThread boolean, madExcept does indeed shows a nice bug report error dialog and stacktrace.
So I think the workaround here might be to detect that we are in the stack overflow state and *not* attempt to pump messages on the main thread in that case.

I now modifed the relevant piece of madExcept.HandleException like so:

Code: Select all

         if (GetCurrentThreadId = MainThreadId) and HandleMessagesInMainThread and
            (hep.exceptionRecord.ExceptionCode <> STATUS_STACK_OVERFLOW) then begin
            while WaitForSingleObject(hep.event, 0) = WAIT_TIMEOUT do
That seems to do the trick and hopefully doesn't have any unwanted side effects.

Interestingly, looking back through our bug database, I see examples of EStackoverflow's being caught by madExcept 4.0.17 and earlier. So it appears to be sensitive to whether Windows can limp along enough to pump the message loop without error. Still, I'd rather not pump the messages and see the madExcept dialogue rather than have Windows silently kill the app regardless of the stack crumbs remaining.
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: Stack overflows not caught by madExcept?

Post by madshi »

Makes sense. Thanks for the suggested code change, I've included it into my sources.
Post Reply