Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a global object (I know it is bad) but when it is destroyed, the destructor is called from the atexit mecanism.
My problem is : this destructor calls some functions and one of them seems to not allow been called from this point (in my case it is the fopen function).
If I known this code is called from atexit, I will avoid calling that function on runtime.

Any idea ?

In fact, the problem is a little more difficult:
- The global object is inside a DLL (linked implicitely with the program)
- in the life of this global object, it may load some other dlls (dynamically)
- in some conditions, it frees the dynamically loaded dlls before application exit (no problem)
- in other condition, the "cleanup" is done by the destructor of the global object.

And my problem is in one of the dynamically loaded DLLs.

if I add atexit function in the dynamically loaded DLL, it will be called when DLL is unloaded (I guess) but the dll is not unloading before the program ends without a call to FreeLibrary.
if I add the atexit function in the global object dll, it will not give access to the flag to the dynamically loaded dlls.
Posted
Updated 5-Jun-14 5:34am
v3
Comments
Richard MacCutchan 5-Jun-14 9:13am    
Since you cannot pass parameters to such a function you could use another global flag that gets set just before the application exits.
Pascal-78 5-Jun-14 9:47am    
I hoped that this flag already exist in the runtime. (I'm using Visual Studio 2010 as compiler)
Pascal-78 5-Jun-14 11:47am    
It seems I was wrong, the dynamically loaded DLL are unloaded before unloading the implicit dll
DllMain with ProcessDetach is called before I enter in my global object destructor.
Any exception in this state is resulting as a program termination without any warning (no cleanup at all !)
Richard MacCutchan 5-Jun-14 11:53am    
I would suggest you rethink your design. The path you are going down with this is likely to lead to more and more trouble.
Pascal-78 5-Jun-14 12:13pm    
Just what I have done: the program needs to call a new function before exit to cleanup the dll (the global object can be changed to an object initialized by a function and destroyed by the explicit cleanup function)

Globals are evil. In your case, you can fight evil with even more evil.
Let's say your code is someting like this:

C++
#include <iostream>
#include <cstdlib>

using namespace std;

X myGlobalX;
//Program calls atexit with FinalFunction
//FinalFunction is executed before program ends
void FinalFunction(void)
{
 
// myGlobalX.~X() is called here. 
}
int main()
{
  atexit(FinalFunction);
}

(source: http://www.cprogramming.com/fod/atexit.html[^]).

You can add a global boolean flag, like this:

C++
#include <iostream>
#include <cstdlib>

using namespace std;

X myGlobalX;
bool calledFromFinalFunction = false; // first change
//Program calls atexit with FinalFunction
//FinalFunction is executed before program ends
void FinalFunction(void)
{
   calledFromFinalFunction = true; // second change
// myGlobalX.~X() is called here; it can look at calledFromFinalFunction and act accordingly.
}
int main()
{
  atexit(FinalFunction);
}


I'm ashamed to even suggest this, but anyway, hope this helps.
 
Share this answer
 
v3
Comments
Pascal-78 5-Jun-14 11:25am    
I guess it will work with the basic level of my question, but the real case is more difficult (I updated my question with more details)
I bypassed the problem by adding a new exported function in the first dll that must be called by the program to perform the cleanup.

It will lead to change the global object to an object initialized by a first call to the dll and destroyed by the explicit cleanup function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900