All raw pointers need to be handled with Smartpointers in a program.
But I'm having problems with this Xaudio2 call
HRESULT XAudio2Create(_Out_ IXAudio2 **ppXAudio2, _In_ UINT32 Flags,
_In_ XAUDIO2_PROCESSOR XAudio2Processor);
My Question is how do you use smart pointers when passing it as a pointer to a pointer and is this even possible? If not how should I go about this in a smart way? I.e how do I pass a smart pointer for the parameter _Out_ IXAudio2 **ppXAudio2
Any Help will be much appreciated.
There are two wrinkles here--first is to handle the fact that the function expects a raw pointer to a (nonconst!) raw pointer, second is to work around the fact that all the built-in smart pointers call
delete
on the owned pointer when what you need to do here is call itsRelease()
method. Waiting to create the smart pointer until after the factory function returns will solve problem 1, and a custom deleter can solve problem 2. Exactly how you want to do things is up to you, but something like this should work:Addendum: There have been tons of COM smart pointers written over the years that do essentially this and also call
AddRef()
/RemoveRef()
when appropriate. e.g. ATL'sCComPtr
. If you have access to one of these, you could use it instead of rolling your own.