×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

CoInitialize hangs

CoInitialize hangs

CoInitialize hangs

(OP)
I have a function which looks like the following-

void x()
{
::CoInitialize();
...
x = getXyz() // returns a pointer to another com object
x.m();

::CoUninitialize();
}

Now, several threads call this function x(). Some times it works fine and some times it hangs idefinitely.

What could be the cause ?

RE: CoInitialize hangs

It probably has something to do with what getXyz() is doing, and/or what you are doing with "x".  Definitely you should not continue to use "x" after the CoUninitialize().

Since you are calling CoInitialize() in a function from several threads, you need to be aware of three important facts:

1. Since you are calling CoInitialize() and not CoInitializeEx(), each of your threads is creating its own single-threaded apartment for any objects you create or marshal into the thread.  You won't be able to share "x" or any other object/interface between threads without marshaling them.  You may want to consider calling CoInitializeEx(NULL,COINIT_MULTITHREADED) instead.

2. Since you are calling CoInitialize() from a function, you should know that a thread can only call CoInitialize() once.  Subsequent calls have no effect.  So if your function is called more than once, it could be a problem.  Generally, you should only call CoInitialize() and CoUninitialize() from your thread's main function, not any subroutines.

3. The first thread that calls CoInitialize() needs to be the last thread to call CoUninitialize().  According to the Microsoft documentation, if you don't do this, subsequent calls to CoInitialize() will fail and the application will not work.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close