• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2357
  • Last Modified:

CoCreateInstance hangs - possible reasons?

Hello there,

Under certain circumstances, a CoCreateInstance call hangs my application. As it seems after some other parts of my program uses COM interfaces and such. I am trying to instanciate an object like this:

      HRESULT hresult;
      CLSID clsid;
      // initialize COM
      hresult = CoInitialize(NULL);

      // get component CLSID
        if (SUCCEEDED(hresult))
           hresult = CLSIDFromProgID(OLESTR("mydll.CMyComClass"), &clsid);
      // the class we defined in dll
      mydll::_CMyComClass *t;
      // create a class instance
      if (SUCCEEDED(hresult))
         hresult = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
            __uuidof(mydll::_CMyComClass), (LPVOID *) &t);

      [ ... ]

Before other COM calls have been made, this code works perfectly. Afterwards, the CoCreateInstance() call hangs my application each and every time. It never comes back. No error message, it's just that nothing happens.

Any ideas? Urgent!
2 Solutions
Hmm, according to MSDN

"Typically, the COM library is initialized on a thread only once. Subsequent calls to CoInitialize or CoInitializeEx on the same thread will succeed, as long as they do not attempt to change the concurrency model, but will return S_FALSE. To close the COM library gracefully, each successful call to CoInitialize or CoInitializeEx, including those that return S_FALSE, must be balanced by a corresponding call to CoUninitialize. However, the first thread in the application that calls CoInitialize(0) or CoInitializeEx(COINIT_APARTMENTTHREADED) must be the last thread to call CoUninitialize(). If the call sequence is not in this order, then subsequent calls to CoInitialize on the STA will fail and the application will not work."

Which basically means that whatever thread called CoInitialize first, it should call CoUninitialize last.  The best way to deal with this is just to call CoInitialize when the App starts up, and call CoUninitialze when it ends.  You really shouldn't be messing with calling this more than once per thread, even if it is valid, sicne it can do some odd things.

Since the return value you are getting will be an S_FALSE, the program thinks this is not a problem, but call in the wrong order and you should end up in a state windows doesn't work in.

There is also this warning:

"CoInitializeEx provides the same functionality as CoInitialize and also provides a parameter to explicitly specify the thread's concurrency model. The current implementation of CoInitialize calls CoInitializeEx and specifies the concurrency model as single-thread apartment. Applications developed today should call CoInitializeEx rather than CoInitialize.

"Because OLE technologies are not thread-safe, the OleInitialize function calls CoInitializeEx with the COINIT_APARTMENTTHREADED flag. As a result, an apartment that is initialized for multithreaded object concurrency cannot use the features enabled by OleInitialize."

So you may want to change the call anyway.

Hope this helps.

if you are doing some multithreading and synchronizations - check you synchronization objects' locks
This may happen because of some waiting for synchronization object ( which is locked ) in the object initialization code

danielssonAuthor Commented:
Thanks guys, I'll try to evaluate your hints in the next couple of days. Right now I've run into a bunch of other things... as always.
On What OS you are running this code?
And Is this code gets executed before the entry point? I mean before main ()?

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now