We help IT Professionals succeed at work.

CoCreateInstance hangs...

amitdedhia
amitdedhia asked
on
I have two threads running in my application. Thread A and Thread B.

Thread A code is like this:

ThreadA()
{
     while (1)
     {
          // do some processing first here
          dword = WaitForSingleObject (hEvent, ?.);
          if (dword == WAIT_OBJECT_0)
               continue;
          else if (dword == WAIT_TIMEOUT)
          {
               Create instance of an InProcServer called IPS using CoCreateInstance();
               // call few functions on IPS
               IPS->Release();
          }
     }
}

Thread B code is like this:

ThreadB()
{
     while (1)
     {
     Create instance of an InProcServer called IPS using CoCreateInstance();;
          // call few functions on IPS
          IPS->Release();
          SetEvent(hEvent); // the event on which thread A is waiting;
          Sleep for some finite time;
     }
}

The problem is as follows. Thread A and thread B are created one after another. If I do not create thread B, everything works well. Thread A periodically creates and releases the inproc server. But if I create therad B, the CoCreateInstance() in thread A does not return (when thread B calls SetEvent(), thread A loops again; waits for some time; times out and tries to create instance of inproc server) and the program hangs. I replaced CoCreateInstance by CoGetClassObject + CreateInstance pair, but the result was same. The code does not even run into DLLGetClassObject function.

Can anybody help me?


Comment
Watch Question

Do you have CoInitialize() at the begining of each thread?

--EC--

Author

Commented:
elcapitan ,
Yes -  For thread A, I have called CoInitiallise(NULL) before calling CoCreateInstance() and CoUniniitalise() after calling Release. Thus, CoInitiallise() and CoUninitiallise() are called in loop everytime the instance of dll is created.

For thread B, CoInitiallise(NULL) and CoUninitiallise() are called outside the loop.

Commented:
What threading models do your client and server use?

Regards,

Steve.

Commented:
What I'm getting at is Multithreaded inproc servers. If your IPS is a multithreaded dll, read Effective COM by Don Box to see why it causes problems.  If not then let me know and I'll keep thinking.

Regards,

Steve.
ekc

Commented:
This is not direct answer to your question, but I think you are spending too much time creating and killing those objects, especialy opening and closing COM in the loop of thread A. Is there a reason not to create the IPS in the process creating the threads, pass the object pointer to both threads and keep it alive while needed? And, of course move the CoInitialize() in the A outside of the loop.
This way, you may resolve your problem indirectly and certainly increase the app's performance...

Author

Commented:
Steve,

My IPS is a multi-threaded DLL. I'll look into Effective COM.

ekc,
actually i determine at runtime which dll to load i.e. IPS is different at different times. I read some information from the registry and get the CLSID of the required IPS. then I call CoCreateInstance(). I can't afford to load all the DLLs in advance.. since they are too many..

Regards
Amit


Commented:
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to refund the points and PAQ at zero points since nobody had an answer for you.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
Per recommendation, points refunded and question closed by
Netminder
CS Moderator