Solved

CoCreateInstance hangs - possible reasons?

Posted on 2004-04-05
5
2,161 Views
Last Modified: 2013-12-03
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!
0
Comment
Question by:danielsson
5 Comments
 
LVL 11

Accepted Solution

by:
KurtVon earned 325 total points
Comment Utility
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.


0
 
LVL 5

Assisted Solution

by:vachooho
vachooho earned 175 total points
Comment Utility
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


0
 
LVL 2

Author Comment

by:danielsson
Comment Utility
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.
0
 
LVL 4

Expert Comment

by:Milind00
Comment Utility
On What OS you are running this code?
And Is this code gets executed before the entry point? I mean before main ()?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now