Stack overflows not caught by madExcept?

delphi package - automated exception handling

Stack overflows not caught by madExcept?

Postby zunzster » Fri Nov 01, 2019 12:52 am

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.
Posts: 51
Joined: Wed Oct 29, 2008 3:43 am

Re: Stack overflows not caught by madExcept?

Postby zunzster » Sun Nov 03, 2019 9:12 pm

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.
Posts: 51
Joined: Wed Oct 29, 2008 3:43 am

Re: Stack overflows not caught by madExcept?

Postby madshi » Wed Nov 13, 2019 9:41 pm

Makes sense. Thanks for the suggested code change, I've included it into my sources.
Site Admin
Posts: 10098
Joined: Sun Mar 21, 2004 5:25 pm

Return to madExcept

Who is online

Users browsing this forum: No registered users and 7 guests