From dc56de6f3014c786af91bfe14207093f9966d1dd Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Sat, 30 Oct 2021 17:53:12 +0100 Subject: [PATCH] Fix memory leak in modules tests --- modules/FilterComponent.cpp | 16 +++------------- modules/FilterComponent.h | 15 +++++++-------- modules/tests/TestModuleCreate.cpp | 4 ++++ 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/modules/FilterComponent.cpp b/modules/FilterComponent.cpp index 2338236..512597e 100644 --- a/modules/FilterComponent.cpp +++ b/modules/FilterComponent.cpp @@ -34,15 +34,12 @@ FilterComponentModule::FilterComponentModule() m_uiNumberOfFrames = 1; m_uiModuleRequirements = ClpModuleFeature::None; m_pchModuleCategory = "Filtering"; - - m_pcFilteredFrame = NULL; } bool FilterComponentModule::createFilter( unsigned int uiWidth, unsigned int uiHeight, unsigned int bitsPixel ) { - m_pcFilteredFrame = NULL; - m_pcFilteredFrame = new CalypFrame( uiWidth, uiHeight, ClpPixelFormats::Gray, bitsPixel ); - return true; + m_pcFilteredFrame = std::make_unique( uiWidth, uiHeight, ClpPixelFormats::Gray, bitsPixel ); + return m_pcFilteredFrame != nullptr; } CalypFrame* FilterComponentModule::filterComponent( CalypFrame* InputFrame, int Component ) @@ -51,14 +48,7 @@ CalypFrame* FilterComponentModule::filterComponent( CalypFrame* InputFrame, int ClpPel*** pppInputPelYUV = InputFrame->getPelBufferYUV(); memcpy( pppOutputPelYUV[CLP_LUMA][0], pppInputPelYUV[Component][0], m_pcFilteredFrame->getWidth() * m_pcFilteredFrame->getHeight() * sizeof( ClpPel ) ); - return m_pcFilteredFrame; -} - -void FilterComponentModule::destroy() -{ - if( m_pcFilteredFrame ) - delete m_pcFilteredFrame; - m_pcFilteredFrame = NULL; + return m_pcFilteredFrame.get(); } FilterComponentLuma::FilterComponentLuma() diff --git a/modules/FilterComponent.h b/modules/FilterComponent.h index 4d92c03..7971b7e 100644 --- a/modules/FilterComponent.h +++ b/modules/FilterComponent.h @@ -31,22 +31,21 @@ class FilterComponentModule : public CalypModuleIf { private: - CalypFrame* m_pcFilteredFrame; + std::unique_ptr m_pcFilteredFrame{ nullptr }; public: FilterComponentModule(); virtual ~FilterComponentModule() {} bool create( std::vector apcFrameList ) = 0; CalypFrame* process( std::vector apcFrameList ) = 0; - void destroy(); bool createFilter( unsigned int uiWidth, unsigned int uiHeight, unsigned int bitsPixel ); CalypFrame* filterComponent( CalypFrame* InputFrame, int Component ); }; -class FilterComponentLuma : public FilterComponentModule +class FilterComponentLuma : public FilterComponentModule, + public CalypModuleInstace { - REGISTER_CLASS_FACTORY( FilterComponentLuma ) public: FilterComponentLuma(); virtual ~FilterComponentLuma() {} @@ -54,9 +53,9 @@ public: CalypFrame* process( std::vector apcFrameList ); }; -class FilterComponentChromaU : public FilterComponentModule +class FilterComponentChromaU : public FilterComponentModule, + public CalypModuleInstace { - REGISTER_CLASS_FACTORY( FilterComponentChromaU ) public: FilterComponentChromaU(); virtual ~FilterComponentChromaU() {} @@ -64,9 +63,9 @@ public: CalypFrame* process( std::vector apcFrameList ); }; -class FilterComponentChromaV : public FilterComponentModule +class FilterComponentChromaV : public FilterComponentModule, + public CalypModuleInstace { - REGISTER_CLASS_FACTORY( FilterComponentChromaV ) public: FilterComponentChromaV(); virtual ~FilterComponentChromaV() {} diff --git a/modules/tests/TestModuleCreate.cpp b/modules/tests/TestModuleCreate.cpp index fc218bd..018c471 100644 --- a/modules/tests/TestModuleCreate.cpp +++ b/modules/tests/TestModuleCreate.cpp @@ -113,6 +113,7 @@ TEST_F( TestModuleCreate, CREATE_OK ) moduleCreated = true; } EXPECT_EQ( moduleCreated, true ); + pcModule->destroy(); } TEST_F( TestModuleCreate, CREATE_FAIL_LESS_ONE ) @@ -129,6 +130,7 @@ TEST_F( TestModuleCreate, CREATE_FAIL_LESS_ONE ) } bool moduleCreated = pcModule->create( apcFrameList ); EXPECT_NE( moduleCreated, true ); + pcModule->destroy(); } } @@ -144,6 +146,7 @@ TEST_F( TestModuleCreate, CREATE_FAIL_PLUS_ONE ) } bool moduleCreated = pcModule->create( apcFrameList ); EXPECT_NE( moduleCreated, true ); + pcModule->destroy(); } } @@ -159,5 +162,6 @@ TEST_F( TestModuleCreate, CREATE_FAIL_NULL_FRAME ) } bool moduleCreated = pcModule->create( apcFrameList ); EXPECT_NE( moduleCreated, true ); + pcModule->destroy(); } }