Re: using NULL pointers in COM

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



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: terminate a PHP script
    ... > Because of the incredible convolution and depth of nested if/then/else> you can get to. ... If I have a function that's scanning a> string looking for and obtaining options and their values for some> command previously scanned off the string, and I detect an error, I want> to set an error code and exit right there. ...
    (comp.lang.php)
  • Re: Various error codes: (was: Add AUXILIARY object class to inetOrgPerson entry)
    ... There are some legal implications of using English strings -- sometimes, ... >> error code as the first hex number of the server error ... >> The problem with returning string errors is that they need to be ... >> With DSIDs -- try searching the KB with the dsid value. ...
    (microsoft.public.windows.server.active_directory)
  • Re: using NULL pointers in COM
    ... *string" if I exit with an error code: ... you exit with an error code. ... pointed-to memory, a crash or heap corruption ensues. ... This has nothing to do with your initializing or not initializing the ...
    (microsoft.public.vc.atl)
  • Re: terminate a PHP script
    ... The Natural Philosopher wrote: ... >> Because of the incredible convolution and depth of nested if/then/else>> you can get to. ... If I have a function that's scanning a>> string looking for and obtaining options and their values for some>> command previously scanned off the string, and I detect an error, I want>> to set an error code and exit right there. ...
    (comp.lang.php)
  • An Error while running Error Handling Code
    ... strCallingProc As String, Optional vParameters, Optional bShowUser As ... Is there something that is wrong in the On Error code? ...
    (microsoft.public.access.modulesdaovba)