Re: COM, returned handles, and memory management

Tech-Archive recommends: Fix windows errors by optimizing your registry



On second thought, how do I pass "an object" across a COM interface?

The cast into / out of unsigned is just a kludge ...

RDeW

"Alexander Nickolov" <agnickolov@xxxxxxxx> wrote in message
news:ef6gdXuuFHA.3932@xxxxxxxxxxxxxxxxxxxxxxx
> I'm surprised nobody yet mentioned that thus is a bad design.
> Without knowing your objective, here's a simple approach
> that may be better: return an object instead of an opaque
> handle (e.g. an interface pointer). You can have a method
> on that object to return you the wrapped handle, but you
> have explicit lifetime management authority by holding the
> object alive. Alternatively, the original object can serve that
> purpose, though that may not fit with the rest of your design.
> BTW, by using a lifetime managemt object you get rid of the
> explicit CloseFoo method as it's not needed anymore.
>
> --
> =====================================
> Alexander Nickolov
> Microsoft MVP [VC], MCSD
> email: agnickolov@xxxxxxxx
> MVP VC FAQ: http://www.mvps.org/vcfaq
> =====================================
>
> "Riley DeWiley" <riley.dewiley@xxxxxxxxx> wrote in message
> news:66Odna3lbpr5YLTenZ2dnUVZ_tGdnZ2d@xxxxxxxxxxxxx
>>I have a COM DLL that is allocating memory and passing back an opaque
>>handle, like this:
>>
>> //allocates memory for new object
>> HRESULT CMyOb::OpenFoo(unsigned * hFoo)
>> {
>> Foo * pFoo = new Foo;
>> *hFoo = (unsigned)pFoo;
>> return S_OK;
>> }
>>
>>
>> //deletes object opened above
>> HRESULT CMyOb::CloseFoo(unsigned hFoo)
>> {
>> if(hFoo)
>> {
>> Foo * pFoo = (Foo *)hFoo;
>> delete pFoo;
>> return S_OK;
>> }
>> else
>> return E_INVALIDARG;
>> }
>>
>> Ignoring for now the obvious issues with the casts, I am noticing that
>> when the client uses it like this:
>>
>> HFOO hFoo = 0;
>> {
>> CComPtr pFoo<IFoo> pFoo;
>> CoCreateInstance(CLSID_FooHousing, CLSCTX_INPROC, etc., (void
>> **)&pFoo);
>> pFoo->OpenFoo(&hFoo);
>> //force pFoo out of scope, decrementing refcount to zero and unloading
>> DLL
>> }
>>
>>
>> {
>> //new CComPtr object in new scope, using old handle
>> CComPtr pFoo<IFoo> pAnotherFoo;
>> CoCreateInstance(CLSID_FooHousing, CLSCTX_INPROC, etc., (void
>> **)&pAnotherFoo);
>> pAnotherFoo->CloseFoo(hFoo); //whammo! A.V. on delete
>> }
>>
>> I get access violations when I hit the delete in CloseFoo(). The Foo
>> object pointed to is landfill. The client has not done anything with the
>> pointer in the meantime. It seems the DLL has unloaded between the two
>> calls and my memory has "gone away", causing the A.V.
>>
>> I want these pointers (handles) to persist. Two questions:
>> 1 - Am I correct in understanding the problem?
>>
>> 2 - What is a workaround, besides either (a) persisting the DLL by
>> holding a pointer to the interface, or (b) having the client own all the
>> memory? Is there a function I can call that will "hold" the memory if the
>> client unloads?
>>
>> RDeW
>>
>>
>>
>>
>>
>
>


.



Relevant Pages

  • Re: DCOM access from a different domain (yes another accessdenied question)
    ... although I am in complete control of the server application ... CoSetProxyBlanket on my test client, ... You need to establish a security blanket for the received interface ... pointer as those credentials are only good for the activation call. ...
    (microsoft.public.win32.programmer.ole)
  • Re: Firing events from thread across proces
    ... Microsoft MVP, MCSD ... > This component gets created from the client(ServerComp).The client then ... > component that is required by the client is created and it's pointer is ... > Now to solve this problem, I passed the marshalled interface pointer ...
    (microsoft.public.vc.atl)
  • Re: COM, returned handles, and memory management
    ... E.g. you pass an interface pointer. ... >>> unloading DLL ... The client has not done anything with the ...
    (microsoft.public.vc.atl)
  • 0x800706f4 A null reference pointer was passed to the stub
    ... I am having some marshalling problems with an IEnumXXXX interface that I ... pointer was passed to the stub when I call the RemoteNext routine. ... server code is a .exe and I have a small console based, C++, test client. ... interface ISoundDevice: IDispatch ...
    (microsoft.public.vc.atl)
  • Re: What doesnt lend itself to OO?
    ... >>server is a pure data transfer interface. ... essentially exposing the client or service implementation. ... >>paradigms can be abstracted just like any other problem space in an OO ...
    (comp.object)