Re: using NULL pointers in COM
- From: "PaulH" <paul.heil@xxxxxxxxx>
- Date: 1 Aug 2006 15:56:48 -0700
Thanks!
If I have a function that takes a VARIANT *, do I have to init that to
NULL also?
I assume I don't have to for built-in types like DWORD * or int *...
-PaulH
Igor Tandetnik wrote:
PaulH <paul.heil@xxxxxxxxx> wrote:
I have a COM object with a method that is declared like this:
[id(1), helpstring("method GetString")] HRESULT GetString([out,retval]
BSTR * string);
In my implementation, it is possible for me to not modify that "BSTR
*string" if I exit with an error code:
COM rules require you to initialize *string (at least to NULL) even if
you exit with an error code. Otherwise it's not clear to your caller
whether or not it's responsible for freeing memory. And the [out]
parameter may legally contain garbage on input, so if it still contains
this garbage on output, _and_ the caller decides it wants to free the
pointed-to memory, a crash or heap corruption ensues.
So simply put this line at the top of your method:
*string = NULL;
But, if BadThings() returns true, then I get an exception whenever the
GetString() function in my COM object is called.
RaiseException: Thread=86c607ac Proc=8041d8c0 'COMTestApp.exe'
AKY=00004001 PC=03fa2ea8(coredll.dll+0x00042ea8)
RA=801078b8(NK.EXE+0x000078b8) BVA=00000000 FSR=00000000
First-chance exception at 0x03fa2ea8 in COMTestApp.exe: Microsoft C++
exception: _com_error at memory location 0x1e11f870..
This has nothing to do with your initializing or not initializing the
parameter. Your client code is using smart wrappers that convert error
HRESULTs into C++ exceptions of type _com_error. The code behaves as
designed. Use try/catch block to handle COM errors.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
.
- Follow-Ups:
- Re: using NULL pointers in COM
- From: Igor Tandetnik
- Re: using NULL pointers in COM
- References:
- using NULL pointers in COM
- From: PaulH
- Re: using NULL pointers in COM
- From: Igor Tandetnik
- using NULL pointers in COM
- Prev by Date: Re: using NULL pointers in COM
- Next by Date: Re: using NULL pointers in COM
- Previous by thread: Re: using NULL pointers in COM
- Next by thread: Re: using NULL pointers in COM
- Index(es):
Relevant Pages
|