From a989914fefc1134186b1fbfef20a9f26a58fcc06 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Sat, 28 Jul 2018 10:25:57 +1000 Subject: [PATCH] [host] remove the invalid usage of SafeRelease SafeRelease was really useless, derefencing the smart pointers through the use of & releases the value before SafeRelease get's to it. Instead either allow the destructor to handle it's release, or explicityly release it by assigning NULL --- host/Capture/DXGI.cpp | 38 +++++++++++++------------------------- host/Com.h | 13 ++----------- host/MFT/H264.cpp | 35 +++++++++++++++-------------------- host/TextureConverter.cpp | 25 ++++++++++++------------- 4 files changed, 42 insertions(+), 69 deletions(-) diff --git a/host/Capture/DXGI.cpp b/host/Capture/DXGI.cpp index 41b58f3a..47371702 100644 --- a/host/Capture/DXGI.cpp +++ b/host/Capture/DXGI.cpp @@ -74,15 +74,13 @@ bool DXGI::Initialize(CaptureOptions * options) output->GetDesc(&outputDesc); if (!outputDesc.AttachedToDesktop) { - SafeRelease(&output); + output = NULL; continue; } m_output = output; if (!m_output) { - SafeRelease(&output); - SafeRelease(&adapter); DEBUG_ERROR("Failed to get IDXGIOutput1"); DeInitialize(); return false; @@ -91,7 +89,6 @@ bool DXGI::Initialize(CaptureOptions * options) m_width = outputDesc.DesktopCoordinates.right - outputDesc.DesktopCoordinates.left; m_height = outputDesc.DesktopCoordinates.bottom - outputDesc.DesktopCoordinates.top; - SafeRelease(&output); done = true; break; } @@ -99,7 +96,7 @@ bool DXGI::Initialize(CaptureOptions * options) if (done) break; - SafeRelease(&adapter); + adapter = NULL; } if (!done) @@ -119,7 +116,6 @@ bool DXGI::Initialize(CaptureOptions * options) D3D_FEATURE_LEVEL_9_1 }; - #define DEBUG 1 #if DEBUG #define CREATE_FLAGS (D3D11_CREATE_DEVICE_DEBUG) #else @@ -137,7 +133,6 @@ bool DXGI::Initialize(CaptureOptions * options) &m_featureLevel, &m_deviceContext ); - SafeRelease(&adapter); #undef CREATE_FLAGS if (FAILED(status)) @@ -315,14 +310,14 @@ void DXGI::DeInitialize() m_pointerBufSize = 0; } - SafeRelease(&m_texture[0]); - SafeRelease(&m_texture[1]); - SafeRelease(&m_texture[2]); - SafeRelease(&m_dup); - SafeRelease(&m_output); - SafeRelease(&m_deviceContext); - SafeRelease(&m_device); - SafeRelease(&m_dxgiFactory); + for(int i = 0; i < _countof(m_texture); ++i) + m_texture[i] = NULL; + + m_dup = NULL; + m_output = NULL; + m_deviceContext = NULL; + m_device = NULL; + m_dxgiFactory = NULL; m_initialized = false; } @@ -435,7 +430,7 @@ GrabStatus Capture::DXGI::GrabFrameTexture(struct FrameInfo & frame, struct Curs break; // no frame data, clean up - SafeRelease(&res); + res = NULL; ReleaseFrame(); // if the cursor has been updated @@ -469,8 +464,7 @@ GrabStatus Capture::DXGI::GrabFrameTexture(struct FrameInfo & frame, struct Curs return GRAB_STATUS_ERROR; } - res.QueryInterface(IID_PPV_ARGS(&texture)); - SafeRelease(&res); + res.QueryInterface(IID_PPV_ARGS(&texture)); if (!texture) { @@ -520,7 +514,6 @@ GrabStatus Capture::DXGI::GrabFrameRaw(FrameInfo & frame, struct CursorInfo & cu return result; m_deviceContext->CopyResource(m_texture[0], texture); - SafeRelease(&texture); result = ReleaseFrame(); if (result != GRAB_STATUS_OK) @@ -560,17 +553,12 @@ GrabStatus Capture::DXGI::GrabFrameYUV420(struct FrameInfo & frame, struct Curso TextureList planes; if (!m_textureConverter->Convert(texture, planes)) - { - SafeRelease(&texture); return GRAB_STATUS_ERROR; - } - SafeRelease(&texture); for(int i = 0; i < 3; ++i) { ID3D11Texture2DPtr t = planes.at(i); - m_deviceContext->CopyResource(m_texture[i], planes.at(i)); - SafeRelease(&t); + m_deviceContext->CopyResource(m_texture[i], t); } result = ReleaseFrame(); diff --git a/host/Com.h b/host/Com.h index 38f4bfb5..e0e12003 100644 --- a/host/Com.h +++ b/host/Com.h @@ -39,6 +39,7 @@ _COM_SMARTPTR_TYPEDEF(IMFActivate , __uuidof(IMFActivate _COM_SMARTPTR_TYPEDEF(IMFAttributes , __uuidof(IMFAttributes )); _COM_SMARTPTR_TYPEDEF(IMFDXGIDeviceManager , __uuidof(IMFDXGIDeviceManager )); _COM_SMARTPTR_TYPEDEF(IMFTransform , __uuidof(IMFTransform )); +_COM_SMARTPTR_TYPEDEF(IMFMediaEvent , __uuidof(IMFMediaEvent )); _COM_SMARTPTR_TYPEDEF(IMFMediaEventGenerator , __uuidof(IMFMediaEventGenerator )); _COM_SMARTPTR_TYPEDEF(IMFMediaType , __uuidof(IMFMediaType )); _COM_SMARTPTR_TYPEDEF(IMFSample , __uuidof(IMFSample )); @@ -52,14 +53,4 @@ _COM_SMARTPTR_TYPEDEF(ID3D11InputLayout , __uuidof(ID3D11InputLayout _COM_SMARTPTR_TYPEDEF(ID3D11VertexShader , __uuidof(ID3D11VertexShader )); _COM_SMARTPTR_TYPEDEF(ID3D11PixelShader , __uuidof(ID3D11PixelShader )); _COM_SMARTPTR_TYPEDEF(ID3D11SamplerState , __uuidof(ID3D11SamplerState )); -_COM_SMARTPTR_TYPEDEF(ID3D11Buffer , __uuidof(ID3D11Buffer )); - - -template void SafeRelease(T **ppT) -{ - if (*ppT) - { - (*ppT)->Release(); - *ppT = NULL; - } -} +_COM_SMARTPTR_TYPEDEF(ID3D11Buffer , __uuidof(ID3D11Buffer )); \ No newline at end of file diff --git a/host/MFT/H264.cpp b/host/MFT/H264.cpp index c5e2d667..8949cf38 100644 --- a/host/MFT/H264.cpp +++ b/host/MFT/H264.cpp @@ -74,7 +74,7 @@ bool MFT::H264::Initialize(ID3D11DevicePtr device, unsigned int width, unsigned ID3D10MultithreadPtr mt(m_device); mt->SetMultithreadProtected(TRUE); - SafeRelease(&mt); + mt = NULL; typeInfo.guidMajorType = MFMediaType_Video; typeInfo.guidSubtype = MFVideoFormat_H264; @@ -131,7 +131,7 @@ bool MFT::H264::Initialize(ID3D11DevicePtr device, unsigned int width, unsigned attribs->GetUINT32(MF_SA_D3D11_AWARE, &d3d11Aware); if (async) attribs->SetUINT32(MF_TRANSFORM_ASYNC_UNLOCK, TRUE); - SafeRelease(&attribs); + attribs = NULL; status = m_mfTransform.QueryInterface(IID_PPV_ARGS(&m_mediaEventGen)); if (FAILED(status)) @@ -179,8 +179,8 @@ bool MFT::H264::Initialize(ID3D11DevicePtr device, unsigned int width, unsigned MFSetAttributeRatio(outType, MF_MT_FRAME_RATE , 60 , 1 ); MFSetAttributeRatio(outType, MF_MT_PIXEL_ASPECT_RATIO, 1 , 1 ); - status = m_mfTransform->SetOutputType(0, outType, 0); - SafeRelease(&outType); + status = m_mfTransform->SetOutputType(0, outType, 0); + outType = NULL; if (FAILED(status)) { DEBUG_WINERROR("Failed to set the output media type on the H264 encoder MFT", status); @@ -200,7 +200,7 @@ bool MFT::H264::Initialize(ID3D11DevicePtr device, unsigned int width, unsigned MFSetAttributeRatio(inType, MF_MT_PIXEL_ASPECT_RATIO, 1 , 1 ); status = m_mfTransform->SetInputType(0, inType, 0); - SafeRelease(&inType); + inType = NULL; if (FAILED(status)) { DEBUG_WINERROR("Failed to set the input media type on the H264 encoder MFT", status); @@ -224,9 +224,9 @@ void MFT::H264::DeInitialize() m_mfTransform->DeleteInputStream(0); } - SafeRelease(&m_mediaEventGen); - SafeRelease(&m_mfTransform); - SafeRelease(&m_mfDeviceManager); + m_mediaEventGen = NULL; + m_mfTransform = NULL; + m_mfDeviceManager = NULL; if (m_encodeEvent) { @@ -240,7 +240,7 @@ void MFT::H264::DeInitialize() if (m_mfActivation) { m_mfActivation->ShutdownObject(); - SafeRelease(&m_mfActivation); + m_mfActivation = NULL; } } @@ -307,7 +307,6 @@ bool MFT::H264::ProvideFrame(ID3D11Texture2DPtr texture) DWORD length; imfBuffer->GetContiguousLength(&length); buffer->SetCurrentLength(length); - SafeRelease(&imfBuffer); IMFSamplePtr sample; MFCreateSample(&sample); @@ -320,8 +319,6 @@ bool MFT::H264::ProvideFrame(ID3D11Texture2DPtr texture) return false; } - SafeRelease(&sample); - SafeRelease(&buffer); return true; } @@ -364,9 +361,11 @@ bool MFT::H264::GetFrame(void * buffer, const size_t bufferSize, unsigned int & memcpy(buffer, pixels, curLen); mb->Unlock(); - SafeRelease(&mb); - SafeRelease(&outDataBuffer.pSample); - SafeRelease(&outDataBuffer.pEvents); + if (outDataBuffer.pSample) + outDataBuffer.pSample->Release(); + + if (outDataBuffer.pEvents) + outDataBuffer.pEvents->Release(); dataLen = curLen; return true; @@ -376,7 +375,7 @@ STDMETHODIMP MFT::H264::Invoke(IMFAsyncResult * pAsyncResult) { HRESULT status, evtStatus; MediaEventType meType = MEUnknown; - IMFMediaEvent *pEvent = NULL; + IMFMediaEventPtr pEvent = NULL; status = m_mediaEventGen->EndGetEvent(pAsyncResult, &pEvent); if (FAILED(status)) @@ -388,14 +387,12 @@ STDMETHODIMP MFT::H264::Invoke(IMFAsyncResult * pAsyncResult) status = pEvent->GetStatus(&evtStatus); if (FAILED(status)) { - SafeRelease(&pEvent); DEBUG_WINERROR("GetStatus", status); return status; } if (FAILED(evtStatus)) { - SafeRelease(&pEvent); DEBUG_WINERROR("evtStatus", evtStatus); return evtStatus; } @@ -403,11 +400,9 @@ STDMETHODIMP MFT::H264::Invoke(IMFAsyncResult * pAsyncResult) status = pEvent->GetType(&meType); if (FAILED(status)) { - SafeRelease(&pEvent); DEBUG_WINERROR("GetType", status); return status; } - SafeRelease(&pEvent); switch (meType) { diff --git a/host/TextureConverter.cpp b/host/TextureConverter.cpp index 5597bf09..e2bde5d2 100644 --- a/host/TextureConverter.cpp +++ b/host/TextureConverter.cpp @@ -271,21 +271,21 @@ bool TextureConverter::IntializeBuffers() void TextureConverter::DeInitialize() { - SafeRelease(&m_samplerState ); - SafeRelease(&m_indexBuffer ); - SafeRelease(&m_vertexBuffer ); + m_samplerState = NULL; + m_indexBuffer = NULL; + m_indexBuffer = NULL; for(int i = 0; i < _countof(m_targetTexture); ++i) { - SafeRelease(&m_shaderView [i]); - SafeRelease(&m_renderView [i]); - SafeRelease(&m_targetTexture[i]); + m_shaderView [i] = NULL; + m_renderView [i] = NULL; + m_targetTexture[i] = NULL; } - SafeRelease(&m_vertexShader); - SafeRelease(&m_psConversion); - SafeRelease(&m_layout ); - SafeRelease(&m_psCopy ); + m_vertexShader = NULL; + m_psConversion = NULL; + m_layout = NULL; + m_psCopy = NULL; } bool TextureConverter::Convert(ID3D11Texture2DPtr texture, TextureList & output) @@ -351,7 +351,7 @@ bool TextureConverter::Convert(ID3D11Texture2DPtr texture, TextureList & output) m_deviceContext->PSSetShader (m_psConversion, NULL, 0); m_deviceContext->DrawIndexed(m_indexCount, 0, 0); - SafeRelease(&textureView); + textureView = NULL; D3D11_RENDER_TARGET_VIEW_DESC targetDesc; ZeroMemory(&targetDesc, sizeof(targetDesc)); @@ -395,7 +395,6 @@ bool TextureConverter::Convert(ID3D11Texture2DPtr texture, TextureList & output) result = m_device->CreateRenderTargetView(dest, &targetDesc, &view); if (FAILED(result)) { - SafeRelease(&dest); delete[] renderViews; DeInitialize(); DEBUG_ERROR("Failed to create the target view"); @@ -411,7 +410,7 @@ bool TextureConverter::Convert(ID3D11Texture2DPtr texture, TextureList & output) m_deviceContext->DrawIndexed(m_indexCount, 0, 0); output.push_back(dest); - SafeRelease(&view); + view = NULL; } delete[] renderViews;