From: Giovanni Dicanio on

"Fred" <not(a)here.com> ha scritto nel messaggio
news:xrGdneOqkOp0GanaRVnyigA(a)pipex.net...

> I found this text in an SDK Imaging demo
>
> // Normally you would only call CoInitialize/CoUninitialize
> // once per thread. This sample calls CoInitialize in this
> // draw function simply to illustrate that you must call
> // CoInitialize before calling CoCreateInstance.
>
> CoInitializeEx(NULL, COINIT_MULTITHREADED);
>
> So I do just that creating it once per thread. And.......
>
> It still fails, so I put a
>
> CoInitializeEx(NULL, COINIT_MULTITHREADED);
>
> CoUninitialize ();
>
> pair around everywhere I CoCreateInstance (CLSID_ImagingFactory
>
> And.......
>
> It sometimes works sometimes doesn't.
> so I imagine it *is* something to do with the threading.

Multithreading is complex, and trying to diagnose it without reading the
source code is harder.

I would suggest you to follow the SDK documentaion advice, i.e. call
CoInitializeEx/Uninitialize once per thread.

Moreoever, COM uses a reference counting also for initialize/uninitialize,
so it is very important that you call CoUninitialize() *only* if
CoInitialize(Ex) was successful, else the reference counter gets fooled.

Moreoever, make sure that you properly release interfaces, so when you do
Release(), clear the pointer setting it to NULL:

if ( pMyInterface != NULL )
{
pMyInterface->Release();
pMyInterface = NULL;
}

Frankly speaking, I would use CComPtr smart pointer...

COM is already complex for its own, and using some helper classes like smart
pointers help develop code more clear, more robust and make it easier to
find bugs...

> The hr is
> hr -2147467259 {E_FAIL}
> The debuuger error I get back when it fails is:
>
> GetImageInfo fails//my text
>
> RaiseException: Thread=9665e4c0 Proc=815875b0 'SP1.exe'

In case of errors of exceptions, also analyzing the call stack may help.

BTW: If you reply to posts, could you please trim the useless text (so the
reader of your post must not read lots of lines...), or at least top-post.
But IMHO bottom posting without trimming is not good...

Giovanni


From: Giovanni Dicanio on

"Fred" <not(a)here.com> ha scritto nel messaggio
news:xrGdneOqkOp0GanaRVnyigA(a)pipex.net...

> It sometimes works sometimes doesn't.
> so I imagine it *is* something to do with the threading.

You may also ask on the ATL public group: microsoft.public.vc.atl

I hope you fix your bugs.

Giovanni


From: Fred on
----- Original Message -----
From: "Giovanni Dicanio" <giovanni.dicanio(a)invalid.it>
Newsgroups: microsoft.public.vc.mfc
Sent: Friday, November 09, 2007 4:53 PM
Subject: Re: Queryinterface then Release fails


>
> "Fred" <not(a)here.com> ha scritto nel messaggio
> news:xrGdneOqkOp0GanaRVnyigA(a)pipex.net...
>

>
> I would suggest you to follow the SDK documentaion advice, i.e. call
> CoInitializeEx/Uninitialize once per thread.



I did and it didn't resove it.


> BTW: If you reply to posts, could you please trim the useless text (so the
> reader of your post must not read lots of lines...), or at least top-post.
> But IMHO bottom posting without trimming is not good...
>

I thought I had been doing so :)

Anyway, Thanks for your pointers - no pun intended. :)


From: Giovanni Dicanio on

"Fred" <not(a)here.com> ha scritto nel messaggio
news:p-mdnRIJnMGIDqnanZ2dnUVZ8qijnZ2d(a)pipex.net...

>> BTW: If you reply to posts, could you please trim the useless text (so
>> the reader of your post must not read lots of lines...), or at least
>> top-post.
>> But IMHO bottom posting without trimming is not good...

> I thought I had been doing so :)
>
> Anyway, Thanks for your pointers - no pun intended. :)

:)

You're welcome

Giovanni