Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the very last place where you can execute logic in a Windows executable?

In my application, I want to execute logic at the very last moment. The later, the better.

The reason for this is that I keep track of certain elements in my application (data structures, resources, ...), and at the end of the application, a routine looks at these elements and reports if they were not correctly closed, freed, deleted, ...

Until now, we did this using several tricks.

The first trick was to overrule the _heap_term function of the C run time (and some other functions as well in the C Run Time). The advantage was that this worked very well, but was limited to applications in which the CRT was statically linked in.

The second trick was to define a global variable like this:

#pragma init_seg(lib)
GlobalApplicationManager s_globalApplicationManager;

The pragma makes sure that this global variable is constructed before all other global variables, and - more important - that it is destructed after all other global variables. This way, we can put the checking logic in the destructor of this class.

Problem is that starting from Windows 7 the destructor is not called anymore in some situations. At this moment it is not clear what influences this, but we are sure that it is not called if we make a successful Oracle connection in our application.

What other tricks are there to execute code as late as possible in an application?

like image 439
Patrick Avatar asked Dec 22 '22 00:12

Patrick


2 Answers

Use /ENTRYPOINT. In your custom entry point, call the CRT entry point and then your final logic.

like image 124
Ben Voigt Avatar answered Dec 28 '22 11:12

Ben Voigt


I think that you should try to refactor your code: Any member variable should be destructed in the relevant class destructor.

for global variables, you may define a destruction function void func(), and call atexit(func) upon initialization.

like image 23
Lior Kogan Avatar answered Dec 28 '22 11:12

Lior Kogan