Delphi: How can I debug access violations when closing my application?

I use Delphi 6, and I have an application that, when turned off, creates access violation errors. We use EurekaLog, so I get stack traces for debugging, but errors appear randomly in different units every time, but always when something is freed up in the finalization section.

How can I debug this to find out what is causing the problem? I'm not sure how to start debugging things that happen when the application terminates.

[Edit:] Sorry if I were unclear, perhaps the best question is: what is the best place to start debugging with breakpoints if I only want to go through the finalization sections? Errors appear to occur in the third-party components that we use (the devexpress dx / cxgrid library), so I would like to start debugging in my code almost at the last moment before Delphi starts to cause the completion of the routines in other units.

+6
source share
2 answers

This is not much, but if I had to guess from past experience ... do you use COM packages or libraries? If you have a global variable that is an interface or an object whose class is declared in the BPL, and you unload the DLL / BPL before clearing the object / interface, you will get access violations because your code is trying to search for VMT in the address space that is larger not displayed in the application.

Check this out and make sure that you clear all such variables before completion begins.

+4
source

When the application closes, do not release things in the finalization section.

1) When the application shuts down, Windows frees up all the application memory. You do not have to do this.

2) When the application shuts down, memory is freed and the infrastructure is unloaded. You cannot call code to close or release objects, as this code may have already been unloaded. You cannot access pointers in memory, as these pointers may already be issued.

3) When you try to free things in the finalization section while the application is closing, you may get crashes that prevent your code from completing, thereby preventing the application from closing, causing the application to freeze and memory loss. This is what you tried to prevent in the first place. Do not do this.

Well, when you work on Win95 / 98 or use external processes, you can in some cases free up shared resources and notify those external processes that you are completing. In addition, all this happens automatically.

-3
source

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


All Articles