ccomoleiunknown

why my IUnknown release function block my child thread?


In my C application, I have a child thread that retrieve a IUnknown interface at beginning of his life :

static struct IUnknown* punk = NULL;

void DispatcherStart(){
    CoInitialize(NULL);
    CheckHRESULT(GetActiveObject(&MY_CLSID,NULL,&punk));
}

everything is fine, it's used to invoke some activeX functions and it work ! however, when my program end, it ask the thread to terminate, so my thread call is ending function's :

void DispatcherStop(){
    if(punk) (punk)->lpVtbl->Release(punk); // BLOCK here
    punk = NULL;
    CoUninitialize();
}

my thead never return because Release on my IUnknow ptr block it. (if I remove the Release then the COUnitialize() block too)

What I am doing wrong ? (the punk iniatilisation can't be done in main thread)


Solution

  • thread that does CoInitialize will fail if it hasn't message pump. So use CoInitializeEx(NULL,COINIT_MULTITHREADED); if thread doesn't have own one.
    But latter case is only for mta com's.
    And this doesn't mean you'll just change CoInitialize to CoInitializeEx..
    mta com should have own base. And you should provide it. like i've done there https://github.com/alexeyneu/tool3/blob/00bfd2aaf2973626f166ea754b756fd0f2fa0d0b/tool3/MainFrm.cpp#L254