From fdc70ac7039d633b26f659e476681d99d45c685a Mon Sep 17 00:00:00 2001 From: Stéphane Lenclud Date: Sun, 3 Feb 2019 09:13:19 +0100 Subject: Kinect: Adding class to encapsulate Microsoft interface pointer. --- tracker-kinect-face/tracker.cpp | 26 +++++++++++++++++++------- tracker-kinect-face/tracker.h | 28 +++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/tracker-kinect-face/tracker.cpp b/tracker-kinect-face/tracker.cpp index 073cf0be..8c2c164d 100644 --- a/tracker-kinect-face/tracker.cpp +++ b/tracker-kinect-face/tracker.cpp @@ -232,6 +232,7 @@ void KinectFaceTracker::ExtractFaceRotationInDegrees(const Vector4* pQuaternion, } + /// /// Initializes the default Kinect sensor /// @@ -248,9 +249,9 @@ HRESULT KinectFaceTracker::InitializeDefaultSensor() if (m_pKinectSensor) { - // Initialize Kinect and get color, body and face readers - IColorFrameSource* pColorFrameSource = nullptr; - IBodyFrameSource* pBodyFrameSource = nullptr; + // Initialize Kinect and get color, body and face readers + UniqueInterface pBodyFrameSource; + UniqueInterface pColorFrameSource; hr = m_pKinectSensor->Open(); @@ -261,17 +262,23 @@ HRESULT KinectFaceTracker::InitializeDefaultSensor() if (SUCCEEDED(hr)) { - hr = m_pKinectSensor->get_ColorFrameSource(&pColorFrameSource); + hr = m_pKinectSensor->get_ColorFrameSource(pColorFrameSource.PtrPtr()); + pColorFrameSource.Reset(); } + + //std::unique_ptr colorFrameSource(pColorFrameSource, SafeRelease); if (SUCCEEDED(hr)) { + //hr = (*pColorFrameSource.PtrPtr())->OpenReader(&m_pColorFrameReader); hr = pColorFrameSource->OpenReader(&m_pColorFrameReader); } + if (SUCCEEDED(hr)) { - hr = m_pKinectSensor->get_BodyFrameSource(&pBodyFrameSource); + hr = m_pKinectSensor->get_BodyFrameSource(pBodyFrameSource.PtrPtr()); + pBodyFrameSource.Reset(); } if (SUCCEEDED(hr)) @@ -297,8 +304,13 @@ HRESULT KinectFaceTracker::InitializeDefaultSensor() } } - SafeRelease(pColorFrameSource); - SafeRelease(pBodyFrameSource); + //pColorFrameSource.Free(); + //pBodyFrameSource.Free(); + //SafeRelease(pColorFrameSource); + //SafeRelease(pBodyFrameSource); + + //SafeRelease(*pBodyFrameSource.PtrPtr()); + //SafeRelease(*pColorFrameSource.PtrPtr()); } if (!m_pKinectSensor || FAILED(hr)) diff --git a/tracker-kinect-face/tracker.h b/tracker-kinect-face/tracker.h index 5068f185..220a126f 100644 --- a/tracker-kinect-face/tracker.h +++ b/tracker-kinect-face/tracker.h @@ -14,7 +14,7 @@ #pragma once -// Safe release for interfaces +// @deprecated Use UniqueInterface instead. Remove it at some point. template inline void SafeRelease(Interface *& pInterfaceToRelease) { @@ -25,6 +25,32 @@ inline void SafeRelease(Interface *& pInterfaceToRelease) } } +template +inline void ReleaseInterface(Interface* pInterfaceToRelease) +{ + if (pInterfaceToRelease != nullptr) + { + pInterfaceToRelease->Release(); + } +} + +// Safely use Microsoft interfaces. +template +class UniqueInterface : public std::unique_ptr)> ///**/ +{ +public: + UniqueInterface() : std::unique_ptr)>(nullptr, ReleaseInterface){} + // Access pointer, typically for creation + T** PtrPtr() { return &iPtr; }; + // Called this once the pointer was created + void Reset() { std::unique_ptr)>::reset(iPtr); } + // If ever you want to release that interface before the object is deleted + void Free() { iPtr = nullptr; Reset(); } +private: + T* iPtr = nullptr; +}; + + class KinectFaceTracker : public ITracker { -- cgit v1.2.3