开发者

Global variable destructors not called, where to start?

开发者 https://www.devze.com 2023-01-11 14:00 出处:网络
I\'m faced with the problem that my applications global variable destructors are not called. This seems to occur only if my application successfully connects to an oracle database (using OCI).

I'm faced with the problem that my applications global variable destructors are not called. This seems to occur only if my application successfully connects to an oracle database (using OCI).

I put some breakpoints in the CRT and it seems that DllMain (or __DllMainCRTStartup) is not called with DLL_PROCESS_DETACH, thus no atexit() is called which explains why my destructors are not called. I have no idea why this happens.

I realize that this is probably not enough information to be able to indicate the cause, but my question is: W开发者_高级运维hat would be a good start to look for the cause of this problem?

This is a list of things I already tried:

  • search the net for solutions
  • attached the debugger and enable native exceptions to see there was no hidden crash, sometimes I get an exception in the .Net framework, but the app seems to continue.
  • try to reproduce in a small application, no success


The most common situation I encounter where this occurs is a program crash. In certain circumstances crashes can happen silently from an end user perspective. I would attach a debugger to the program, set it to break on all native exceptions and run the scenario.


It's possible that someone is calling TerminateProcess, which unlike ExitProcess does not notify DLLs of shutdown.


This might be helpful:

What happens to global variables declared in a DLL?

Are your globals declared inside a dll, or in your application's memory space?


Calling theexit API often means that the application exits without calling destructors. I an not sure that VC does in this case.

Also try avoid using global objects. This is because you have little control over when and in what order the constructors and destructors are called. Rather convert the objects into pointers and initialise and destroy the pointers using the appropriate DllMain hooks. Since DllMain is an OS construct and not a language construct, it should prove more reliable in the case of normal exits.

0

精彩评论

暂无评论...
验证码 换一张
取 消