How does a .NET program behave differently at startup and without debugging in Visual Studio?

I get an access violation in DBEXPSDA40.DLL (Dev Art MS SQL Server dbexpress) when closing my .NET application. My application (VB.NET) calls a Delphi written COM server that uses dbexpress to connect to SQL Server.

If I do the same, but my host application is a native Delphi or Excel VBA application, then I do not see A / V. I also do not see this if I run the VB.NET application in VS IDE with debugging.

I tracked A / V to the finalization clause in the dbexpress block, which takes care of closing the drivers (in this case, two, one for SQL Server and one for SQL Server Compact)

If I can understand what the difference is between debugging and the absence in the .NET environment, I probably know where to look next.

+4
source share
2 answers

Your difference in memory layout.

The process is influenced by many subtle factors. First, in the debugger, the JIT generates a slightly different code (to host the debugger). Depending on your debugger’s settings, Visual Studio may also inject other code into your process (for example, .vshost.exe). The debugger can also affect the time and, in turn, can set the race conditions and / or change the memory allocation.

In short, by the time you close the application, you will get a [slightly or significantly] different memory structure. The same goes for the other host application.

But this is only one side of the story. The other side is a mistake in dbexpress. Or maybe some other module causes memory corruption in dbexpress data. In any case, dbexpress gains access to some random address.

And this address is simply located on an unallocated memory page in one case, but happens to be in a dedicated case in other cases (because the memory layout is different, remember?). And in the latter case, dbexpress simply reads the value from memory, does something with it, apparently satisfied with the result and exits gracefully.

This (along with unsurpassed race conditions) is a very common problem with immaturely written unmanaged code (which, as my experience shows, is very common when using Delphi).

Decision? Change the conditions. You can try another car. Or on the same machine, but under heavy load. Or download some more modules. Or don't download some modules that you usually do. Play with him.

Saying this, yours truly doesn’t go that way. He's just looking for a pin in a haystack, never ending, emotionally draining an adventure. In addition, you are highly likely to receive AV elsewhere (but due to the same underlying reason).

Another (and best) option is debug printing. That is, if you have the dbexpress source code (sorry, I am not familiar with it).

Otherwise, I would start with a very thorough review of the code for the Delphi component. And perhaps debugging will also be printed.

Good luck.

+1
source

Perhaps the host application is hiding an exception from you. Shutdown errors are more complicated. Add a few logs to see if an exception occurred when the debugger is disconnected.

0
source

Source: https://habr.com/ru/post/1302142/


All Articles