From 14e75a52c6696573e0918b6a43998580df8c313e Mon Sep 17 00:00:00 2001 From: Sahab Yazdani Date: Sat, 17 Dec 2011 17:00:24 -0500 Subject: [PATCH] Issue #2: Release AM_MEDIA_TYPE structures when no longer being used. --- DSGrab.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/DSGrab.cpp b/DSGrab.cpp index d2e93d2..0265475 100644 --- a/DSGrab.cpp +++ b/DSGrab.cpp @@ -54,7 +54,7 @@ #include // boost -#include +#include typedef std::basic_string tstring; typedef std::basic_stringstream tstringstream; @@ -69,6 +69,45 @@ typedef std::basic_stringstream tstringstream; static ULONG gdiplusToken; +// The following functions are defined in the DirectShow base class library. +// They are redefined here for convenience, because many applications do not +// need to link to the base class library. +// These functions were copied from: http://cppxml.googlecode.com/svn-history/r16/trunk/englishplayer/EnTranscription/dshowutil.h +// Which in turn were copied from: http://msdn.microsoft.com/en-us/library/windows/desktop/dd375432%28v=VS.85%29.aspx +#ifndef __STREAMS__ + +// FreeMediaType: Release the format block for a media type. +inline void FreeMediaType(AM_MEDIA_TYPE& mt) +{ + if (mt.cbFormat != 0) + { + CoTaskMemFree((PVOID)mt.pbFormat); + mt.cbFormat = 0; + mt.pbFormat = NULL; + } + if (mt.pUnk != NULL) + { + // Unecessary because pUnk should not be used, but safest. + mt.pUnk->Release(); + mt.pUnk = NULL; + } +} + +// DeleteMediaType: +// Delete a media type structure that was created on the heap, including the +// format block) +inline void DeleteMediaType(AM_MEDIA_TYPE *pmt) +{ + if (pmt != NULL) + { + FreeMediaType(*pmt); + CoTaskMemFree(pmt); + } +} + + +#endif + namespace Exception { class NoSuchDevice { }; @@ -166,7 +205,7 @@ void CaptureDevice::EnumerateDeviceCaps() { _T( "\tBit Depth:" ) << bitDepth << std::endl; - // free media_type + DeleteMediaType(media_type); } streamConfig->Release(); @@ -200,7 +239,7 @@ Gdiplus::Bitmap *CaptureDevice::GetSingleSnapshot( DWORD wait /* = 0 */ ) { hr = sampleGrabber->SetMediaType( &am ); - // TODO: release sourceAM + DeleteMediaType(sourceAM); streamConfig->Release(); } else {