Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

c - 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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...