PDA

View Full Version : Help! the Try... block in a win32 dll doesn't catch the AVs!



Chebmaster
09-12-2007, 04:12 AM
I put most of my code in an dll which the main program calls every frame. Of course an exception should never cross the dll boundary, so it is caught by try...except block and reported using different means.

The most troubling part, this paradigm works ideally for Linux, but not for Win32. Both the Wine and WinXP, both the fpc 2.0.4 and 2.2.0 show the same behavior: the try...except block is as effective against the Access Violation as a wet tissue paper.

In short, a simple byte(nil^):=0; becomes lethal.

Am I doing something wrong? The program exceptions created by the raise operator are caught normally.

P.S. The divisions by zero aren't caught too.

AthenaOfDelphi
09-12-2007, 09:45 AM
First up, I recall reading somewhere that if the DLL doesn't use sysUtils, exceptions may break it and the calling app, so make sure that the DLL includes sysUtils.

Secondly, this is probably stating the obvious, but don't rely on exceptions for handling potential problems. Make the code in the DLL far more robust and include checks for assigned pointers, division by 0 etc. and take action with conditional flow control. You may already be doing this and using exceptions as a last resort... in which case make your calling code more robust in dealing with potential issues.

Thats all I can think of at the mo hon, hope it helps (although I should add, I'm not sure how relevant this is to fpc).

Chebmaster
10-12-2007, 08:31 AM
if the DLL doesn't use sysUtils,
It does :cry:


, but don't rely on exceptions for handling potential problems.
I don't really rely on them, I use the nested try... blocks for debugging, where each except block adds its words to the log and re-raises the exception.

Being unable to catch AVs would complicate the debugging immensely, making all this mechanism useless.
It seems, I must think more and re-check carefully if the memory gets trashed somewhere. Maybe the exception handling mechanisms in the exe and dll conflict somehow? :( After all, the dll calls the functions exported from the exe a lot, and all of them are wrapped internally in the try... blocks.

Damn. I knew I should have used interfaces!

Chebmaster
15-12-2007, 11:15 AM
This behavior is abnormal as writing the small test program showed.
The Delphi DLL and FPC .so for Linux catch AVs inside them just finely.

I reported this as FPC bug: http://www.freepascal.org/mantis/view.php?id=10421