COM Singleton object and static members lifetime



Hello everybody
Here is a question about COM singleton object and static members
lifetime.

The problem:
I have a COM Singleton object in a DLL that is instantiated in a EXE
application. When I close the EXE app, the DLL is unloaded and a call
to FinalRelease() function is executed for a COM Singleton object
before it is destroyed . The problem is that when the code tries to
access some static members during this time, the application crashes.

The question:
What is the lifetime of the static members in this case? Can we assume
that static members are still valid when the COM singleton object's
FinalRelease() function is called?

More explanation:
We were using a previous version of ATL before and it was working fine.
But the problem appeared I think when we ported the code to VS2005 with
ATL 8.0.

Here is a description of what is happening. You can see the stack below
this description.

1) OmniDir.exe is the EXE application. You can see in the stack that it
is closing, then some calls are made in msvcr80d.dll and other DLLs.

2) After that, my DLL GxResource.dll is unloaded when
DllMainCRTStartup() is called.

3) This eventually calls FinalRelease() on my COM singleton object
CResource before it is destroyed

4) The CDisplayHelper::Terminate() static function is finally called
and this function tries to access static members which in turn crash
the application because these members are invalid at this point.

CDisplayHelper::Terminate()
CAppResources::Terminate()
COmnicastAppResources::Terminate()
CResources::DestroyResources()
CResources::FinalRelease()
ATL::CComObjectCached<CResources>::~CComObjectCached<CResources>()
ATL::CComObjectCached<CResources>::`scalar deleting destructor'()
TL::CComObjectCached<CResources>::Release()
ATL::CComPtrBase<IUnknown>::~CComPtrBase<IUnknown>()
ATL::CComPtr<IUnknown>::~CComPtr<IUnknown>()
ATL::CComClassFactorySingleton<CResources>::~CComClassFactorySingleton<CResources>()

ATL::CComObjectCached<ATL::CComClassFactorySingleton<CResources>
::~CComObjectCached<ATL::CComClassFactorySingleton<CResources> >()
ATL::CComObjectCached<ATL::CComClassFactorySingleton<CResources>
::`scalar deleting destructor'()
ATL::CComObjectCached<ATL::CComClassFactorySingleton<CResources>
::Release()
ATL::CAtlComModule::Term()
ATL::CAtlComModule::~CAtlComModule()
ATL::`dynamic atexit destructor for '_AtlComModule''()
_CRT_INIT(void * hDllHandle=0x07710000, [...])
__DllMainCRTStartup(void * hDllHandle=0x07710000, [...])
_DllMainCRTStartup(void * hDllHandle=0x07710000, [...])

[...]

msvcr80d.dll!__crtCorExitProcess(int status=0)
msvcr80d.dll!__crtExitProcess(int status=0)
msvcr80d.dll!doexit(int code=0, int quick=0, int retcaller=0)
msvcr80d.dll!exit(int code=0)
OmniDir.exe!__tmainCRTStartup()
OmniDir.exe!wWinMainCRTStartup()

Any idea on what is happening here?

Best regards,
Jonathan

.



Relevant Pages

  • Re: Creating global objects in a class library
    ... You don't need to instantiate class to use it's static members. ... You reference your dll exactly in same manner as you reference System.IO or ... I need to be able to have all my functions read/write data from the ...
    (microsoft.public.dotnet.languages.vc)
  • Load Dll at Singleton
    ... I have created a singleton object that has a hashtable which holds ... functions every time i want without having to open each time the dll. ... The problem is that when i do this i cannot debug my project at all, ... I cannot debug even for eg. the load event of a ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Singleton Objects Dying
    ... > Maybe the objects are not getting cleaned off of the server? ... > replace the DLL once it hangs the object calls start getting answered ... That gives the singleton object an infinite lifetime. ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: DLL export of STL map doesnt work between VC++ 6.0 and VC 7.1
    ... > classes have static members they use for internal ... > you'll have an exe copy of those statics as well as a DLL ... In STL implementation shipped with VC6, ... consider applying all the other fixes while you are at it. ...
    (microsoft.public.vc.stl)
  • Re: Simple Managed DLL Creation
    ... Use static members of a class instead. ... Ronald Laeremans ... > If you were to reference this dll in .net, I doubt a global or static ...
    (microsoft.public.dotnet.languages.vc)