Application.OnException doesn't work in 64-bit? lazarus / 21-General ---------------------------------------------------------------------- mbisax · 2021-03-27 10:18 Hi, I hope to be in the right section. I have a problem in a project that I can't solve. I overwrite the TForm's OnExcpet event but compiling in 64-bit, the associated procedure is never called. Compiling the project to 32-bit, everything works correctly. I'm attaching a small sample project where you can check the problem. I use Lazarus 2.0.8 and fpc 3.2.0 but, I also tried with fpc 3.04 and 3.02 with the same result. Has anyone had the same problem and managed to find a solution ?. I am indebted to you for all advice. ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-03-27 11:20 Hey mbisax, I'm testing this on Lazarus 2.1.0 trunk and FPC 3.3.1 on Ubuntu 20.10 64b and I get the exception. Problem is that I'm not getting the exception text you trigger when using the timer. If I add a button and trigger the exception from there, then I get the 'Test Exception!' text. Somehow, by using a timer on my test the exception gives the text 'Access violation'. But I'm guessing that's because the timer is on another thread and that can mess stuff about. Oddly enough under Lazarus 2.0.12 and FPC 3.2.0 I get the 'Test Exception!' text on both the button and the timer. Oddly enough under Lazarus 2.0.10 and FPC 3.2.0 I also get the 'Test Exception!' text on both the button and the timer. Oddly enough under Lazarus 2.0.6 and FPC 3.0.4 I also get the 'Test Exception!' text on both the button and the timer. All this in a Linux 64b environment. Since I don't have a Windows machine I tested under wine and neither 32 nor 64 under wine gave me the exception with the timer. However with the button I always got the exception. Hope this helps!! Cheers, Gus ---------------------------------------------------------------------- Bart · 2021-03-27 11:23 Exceptions raise in an OnTimer event seem to not bubble up somehow. Bart ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-03-27 11:23 Hey mbisax, Oh, and your title is misleading since on your test, you're using Application.OnException and not TForm.OnException. Actually, I don't think that TForm.OnException even exists... Cheers, Gus ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-03-27 11:25 Hey Bart, Quote from: Bart on March 27, 2021, 12:23:05 pmExceptions raise in an OnTimer event seem to not bubble up somehow. I guess you're talking about Windows. As you can attest from my post, they do under Linux Cheers, Gus ---------------------------------------------------------------------- mbisax · 2021-03-27 12:01 Quote from: gcarreno on March 27, 2021, 12:23:50 pmHey mbisax, Oh, and your title is misleading since on your test, you're using Application.OnException and not TForm.OnException. Actually, I don't think that TForm.OnException even exists... Cheers, Gus yes, sorry, now I change it right away. ---------------------------------------------------------------------- mbisax · 2021-03-27 15:01 bart, you're right !. In fact, from further tests made it seems that the problem lies precisely in an exception that occurs inside an timer. In my Windows 10 at 32 bit it works, at 64 no !!! Attached the test prg to try (in this program I have included some changes recommended by a friend). ---------------------------------------------------------------------- mbisax · 2021-03-27 15:07 Quote from: gcarreno on March 27, 2021, 12:20:41 pmHey mbisax, I'm testing this on Lazarus 2.1.0 trunk and FPC 3.3.1 on Ubuntu 20.10 64b and I get the exception. Problem is that I'm not getting the exception text you trigger when using the timer. If I add a button and trigger the exception from there, then I get the 'Test Exception!' text. Somehow, by using a timer on my test the exception gives the text 'Access violation'. But I'm guessing that's because the timer is on another thread and that can mess stuff about. Oddly enough under Lazarus 2.0.12 and FPC 3.2.0 I get the 'Test Exception!' text on both the button and the timer. Oddly enough under Lazarus 2.0.10 and FPC 3.2.0 I also get the 'Test Exception!' text on both the button and the timer. Oddly enough under Lazarus 2.0.6 and FPC 3.0.4 I also get the 'Test Exception!' text on both the button and the timer. All this in a Linux 64b environment. Since I don't have a Windows machine I tested under wine and neither 32 nor 64 under wine gave me the exception with the timer. However with the button I always got the exception. Hope this helps!! Cheers, Gus Yes, it happens to me too. Equal! Raising an exception in a Timer with Linux works, in Windows 32 it works at 64 NO! ---------------------------------------------------------------------- PascalDragon · 2021-03-27 15:50 FPC 3.0.x on Win64 and FPC 3.2.x on Win32 use Windows Structured Exception Handling for exception handling instead of the setjmp/longjmp based one which was the default before that and which is still the default on other platforms like Linux. This way FPC (and also Delphi) is fully compatible with the exception handling mechanism that is used by Windows. This however also implies that Windows might catch and swallow FPC exceptions to protect its own state and this is exactly what happens here: The LCL uses the Windows SetTimer API which leads to the timer function being executed from Windows' default message handler in user32.dll and this protects the called code using a SEH protection block (essentially a try ... catch) - one can see this in ReactOS' source code, but also when debugging with e.g. WinDbg. This however also means that the exception will not propagate back to the application and thus FPC's exception handler will never see it. ---------------------------------------------------------------------- mbisax · 2021-03-27 16:01 Thanks PascalDragon, thanks for the enlightening explanation !. I understand, therefore, that there is no possible solution. Do you think we can get around this with some programming trick? I hang from your lips ---------------------------------------------------------------------- PascalDragon · 2021-03-27 16:43 Quote from: mbisax on March 27, 2021, 05:01:13 pmI understand, therefore, that there is no possible solution. Correct, at least not with exceptions in a transparent way. Quote from: mbisax on March 27, 2021, 05:01:13 pmDo you think we can get around this with some programming trick? Adjust your timer handler like this (I assume that your real code that raises the exception is more complex): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i try  // make sure that the timer isn't enabled should an exception fire  Timer1.Enabled := False;  // your code which I assume is more complex ;)  raise Exception.Create('Timer');  // now you can enable the timer again if necessary  Timer1.Enabled := True;except  Application.HandleException(Self);end; This will also work for non-Windows platforms. ---------------------------------------------------------------------- mbisax · 2021-03-27 21:03 It works!! In windows, 32 or 64, Linux 32 or 64 .... it works perfectly. I hadn't thought of calling the HandleException (self) !. Thank you very much: PascalDragon, I owe you. ---------------------------------------------------------------------- mbisax · 2021-03-28 09:25 For all those who have followed this post, especially for those who have taken the time to help and explain, I post the final source with the changes suggested by PascalDragon. Thanks very much to all. ---------------------------------------------------------------------- PascalDragon · 2021-03-28 09:26 Quote from: mbisax on March 27, 2021, 10:03:31 pmI hadn't thought of calling the HandleException (self) !. To be fair I had to check as well how the various pieces fit together here. ---------------------------------------------------------------------- mbisax · 2021-03-28 10:07 Quote from: PascalDragon on March 28, 2021, 11:26:05 amQuote from: mbisax on March 27, 2021, 10:03:31 pmI hadn't thought of calling the HandleException (self) !. To be fair I had to check as well how the various pieces fit together here.