Solved

CoCreateInstance hangs - possible reasons?

Posted on 2004-04-05
5
2,201 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

860 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