pointer to pointer intialize to NULL but still point to NULL

C

Christopher

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?
 
S

Stuart Redmann

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
 
J

Juha Nieminen

Stuart Redmann said:
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.
 
S

Stuart Redmann

  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
 
R

Ruben Safir

Christopher said:
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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top