pointer to pointer intialize to NULL but still point to NULL

Discussion in 'C++' started by Christopher, Jul 7, 2011.

  1. Christopher

    Christopher Guest

    void Foo(IUnknown ** ppMicrosoftsOut)
    {
    // If the operation fails param is NULL

    // Otherwise param is a valid pointer to object
    }


    I can't change the function prototype, because there is already a lot
    of code that depends on it. How do I initialize the parameter properly
    in the function body, such that all of the following calls from the
    client yield the expected results?

    1) Foo(NULL);
    2) IUnknown * result = somePointerToAlreadyInstantiatedObject;
    Foo(&result);
    3) IUnknown * result = NULL;
    Foo(&result);

    This is my attempt:

    if( ppRS && *ppRS )
    {
    (*ppRS)->Release();
    (*ppRS) = NULL;
    }

    // Do stuff

    // If failed
    return;

    // If successfull
    *ppRS = pointerToInterface;

    Is that correct?
     
    Christopher, Jul 7, 2011
    #1
    1. Advertising

  2. On 8 Jul., Christopher wrote:
    > void Foo(IUnknown ** ppMicrosoftsOut)
    > {
    > // If the operation fails param is NULL
    >
    > // Otherwise param is a valid pointer to object
    >
    > }
    >
    > I can't change the function prototype, because there is already a lot
    > of code that depends on it. How do I initialize the parameter properly
    > in the function body, such that all of the following calls from the
    > client yield the expected results?


    Note that this is a MS COM specific question that is off-topic here.
    You should try microsoft.public.vc.mfc, that is the only group that
    shows at least some activity (the dedicated COM groups were never very
    active, even back in the time when MS had not declared its groups
    dead) or use the social.msdn site (they dropped their newsgroup in
    favour of this credit-driven web-based pseudo-newsgroup).

    > 1) Foo(NULL);


    That is a case that you should not be bothered with because the rules
    of COM actually forbid that an [out]-parameter is initialized with a
    NULL pointer. The correct answer to such a case would be
    HRESULT Foo (IUnknown ** ppMicrosoftsOut)
    {
    if (!ppMicrosoftsOut)
    return E_POINTER;

    // whatever.
    return S_OK;
    }

    Note that you would have to make the method more COM-compliant by
    changing the return type into an HRESULT.

    > 2) IUnknown * result = somePointerToAlreadyInstantiatedObject;
    > Foo(&result);
    > 3) IUnknown * result = NULL;
    > Foo(&result);
    >


    This depends on whether the parameter ppMicrosoftsOut is declared as
    [in, out] or just as [out] parameter. Pointer parameters that are
    [out] and not also [in] should be initialized with a NULL pointer be
    the caller, so you could write:

    HRESULT Foo (IUnknown ** ppMicrosoftsOut)
    {
    if (!ppMicrosoftsOut)
    return E_POINTER;

    ASSERT (!*ppMicrosoftsOut);

    // whatever.
    return S_OK;
    }


    > This is my attempt:
    >
    > if( ppRS && *ppRS )
    > {
    > (*ppRS)->Release();
    > (*ppRS) = NULL;
    >
    > }
    >
    > // Do stuff
    >
    > // If failed
    > return;
    >
    > // If successfull
    > *ppRS = pointerToInterface;
    >
    > Is that correct?


    Yes, that should always work (for both the [out] and the [in, out]
    case).


    I strongly recommend that you use the automatically generated wrappers
    that you get when you #import type libraries (you'll get those .tlh
    and .tli files). They use a smart pointer _com_ptr_t that relieve you
    from having to manage the refcount of COM pointers.

    Regards,
    Stuart
     
    Stuart Redmann, Jul 8, 2011
    #2
    1. Advertising

  3. Stuart Redmann <> wrote:
    > Note that this is a MS COM specific question that is off-topic here.


    How exactly? I didn't see anything in the posted code that couldn't be
    just regular standard C++ (when the used names are defined properly).

    Just because a variable name happens to contain the word "Microsoft" in
    it doesn't mean the posted code is "MS COM specific". He could have changed
    it to something else and you would be none the wiser.
     
    Juha Nieminen, Jul 8, 2011
    #3
  4. Stuart Redmann wrote:
    > > Note that this is a MS COM specific question that is off-topic here.


    On 8 Jul., Juha Nieminen wrote:
    >   How exactly? I didn't see anything in the posted code that couldn't be
    > just regular standard C++ (when the used names are defined properly).


    Really? I thought that at least _you_ would recognize a COM specific
    question if you saw one.

    >   Just because a variable name happens to contain the word "Microsoft" in
    > it doesn't mean the posted code is "MS COM specific". He could have changed
    > it to something else and you would be none the wiser.


    Then I'd have said that I would need to know the definition of
    IUnknown, and why he had called Release on it. That's how _I_ spotted
    that it is a COM specific question.

    If I had told him that from a C++ point of view it is totally OK to
    pass a NULL pointer into Foo, he would not not have gained much
    information (I think he already knows that).

    I know that I should have refrained from answering when I already had
    spotted that this question is kind of off-topic here. I didn't because
    I am probably the last Knight of COM (all the other members of Round
    Table have joined the Kingdom of DotNet), so I'm happy to see some new
    followers.

    SCNR,
    Stuart
     
    Stuart Redmann, Jul 8, 2011
    #4
  5. Christopher

    Ruben Safir Guest

    Christopher <> wrote:
    > void Foo(IUnknown ** ppMicrosoftsOut)
    > {
    > // If the operation fails param is NULL
    >
    > // Otherwise param is a valid pointer to object
    > }
    >
    >
    > I can't change the function prototype, because there is already a lot
    > of code that depends on it. How do I initialize the parameter properly
    > in the function body, such that all of the following calls from the
    > client yield the expected results?
    >


    My expereince is that C++ despises null...
     
    Ruben Safir, Jul 9, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmF6eg==?=
    Replies:
    1
    Views:
    512
    Trevor Benedict R
    Jun 13, 2004
  2. aneuryzma
    Replies:
    3
    Views:
    761
    Jim Langston
    Jun 16, 2008
  3. Malcolm Nooning

    Why is the pointer passed into the function still NULL?

    Malcolm Nooning, Apr 5, 2011, in forum: C Programming
    Replies:
    8
    Views:
    372
    Malcolm Nooning
    Apr 21, 2011
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,389
    Saraswati lakki
    Jan 6, 2012
  5. G G
    Replies:
    33
    Views:
    266
    Michael Angelo Ravera
    May 3, 2014
Loading...

Share This Page