Re: using NULL pointers in COM

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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

.



Relevant Pages

  • Re: using NULL pointers in COM
    ... Is there a way to set the error string returned with the _com_error ... pointed-to memory, a crash or heap corruption ensues. ... exception: ... This has nothing to do with your initializing or not initializing the ...
    (microsoft.public.vc.atl)
  • Re: MASD syntax to load the current address in C.A at runtime
    ... so my memory on A=PC is not as good. ... CODE object, the CRC on the CODE object will change. ... easier to store individual pieces as the time string was built. ...
    (comp.sys.hp48)
  • Re: Fast string operations
    ... Looping: I thought looping over arrays in managed code was "slow" ... array handling and such. ... The problem with TrimHelper is that it always returns a new string instance. ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Discovering variable types...
    ... >- but I suppose MS expect us to use wrappers ... memory allocations for your variables from disk as well. ... >They most certainly are of fixed size, changing the size of a String ... >>me to keep buffer size and current postion right in the memory block. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Optimize
    ... if you use it like me to get 'the next string ptr' in addition. ... | |mov ebx eax;dw aligned strings are faster ... | Would it be enough to touch each 16th bytes in the 64K memory area ... Cache-line size is fixed, my AMD got 64 bytes per line. ...
    (alt.lang.asm)