Re: CoUninitialize() and CComPtr<> object lifetimes



"Riley DeWiley" <riley.dewiley@xxxxxxxxx> wrote in message
news:P4qdnS3RU5Ct8k3fRVn-hQ@xxxxxxxxxx
> I have tracked down a bug involving object lifetimes to a quirk in
> CComPtr<> that is related to CoInitialize() and CoUninitialize(). It
> seems that the destructor for my CComPtr<> is not being called until
> CoUnitialize() is called, even though the variable itself has gone
> out of scope. It does not help that CoInitialize() is being called
> somewhere in the app framework.
> Here is the issue, it appears in an OnInitDialog() fxn for a modal
> dialog:
> LRESULT OID( ... blah ...)
> {
> //CoInitialize() has already been called ...
> CComPtr<IMyOb> pOb = 0; //local declaration of ccomptr, OK
> HRESULT hr = CoCreateInstance( ..., inproc|local server,
> IID_IMyOb, &pOb ...);
> ..error check on hr ...
> pOb->foo();
> return SOMETHING;
> }
>
> With the code above, I would expect that the dtor for _Module would
> fire right after the return() statement

You would be wrong. _Module is a global variable, its constructor is
called when the DLL is loaded, and its destructor - when the DLL is
unloaded. COM does not unload the DLL as soon as the last object is
released. Left to its own devices, COM would never unload a DLL it
loaded until CoUninitialize. But see CoFreeUnusedLibraries
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • CoUninitialize() and CComPtr<> object lifetimes
    ... I have tracked down a bug involving object lifetimes to a quirk in CComPtr<> ... help that CoInitialize() is being called somewhere in the app framework. ... CoUninitialize(); ... What is going on with this DLL? ...
    (microsoft.public.vc.atl)
  • JNI and COM problem
    ... I have a JNI DLL that is statically linked to an MFC DLL. ... // the Java side ... CoInitializeEx and CoUninitialize pair ... For example is it possible to create a worker thread in the Java side ...
    (comp.lang.java)
  • Re: operator new[] fails in DLL when being called from within VB6 process
    ... its initialization graph, it may work one day, and the next day you'll start ... DllMain being called before ole32's DllMain. ... > But btw - calling CoInitialize() in DllMainis actually very forbidden ... >> I can't remember the details, and our DLL is a complex multi-threaded RPC ...
    (microsoft.public.win32.programmer.kernel)
  • Re: operator new[] fails in DLL when being called from within VB6 process
    ... and our DLL is a complex multi-threaded RPC ... importing our standard client/server WINAPI based dlls. ... You would call CoInitialize() in DLL_PROCESS_ATTACH and you would call ... > loaded into a VB6 process? ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Calling a Activex control from a DLL
    ... Note that CoInitialize does explicitly state that it cannot be called from the DllMain ... CoInitialize/Unitializein the DLL anyway? ... MVP Tips: http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)