Solved

CoCreateInstance hangs - possible reasons?

Posted on 2004-04-05
5
2,175 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
ID: 10757351
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
ID: 10760730
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
ID: 10763404
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
ID: 10822903
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

22 Experts available now in Live!

Get 1:1 Help Now