Memory Management : Adjustment and fixes

Add release of several static singleton objects
Fix several minor memory leaks
This commit is contained in:
Magne Sjaastad
2022-03-03 10:15:32 +01:00
parent 7a2a297648
commit 0e57cfe201
36 changed files with 441 additions and 141 deletions

View File

@@ -68,9 +68,9 @@ endif()
# Defining all the source (and header) files
# ##############################################################################
set(CODE_HEADER_FILES)
set(CODE_HEADER_FILES RiaMainTools.h)
set(CODE_SOURCE_FILES RiaMain.cpp)
set(CODE_SOURCE_FILES RiaMain.cpp RiaMainTools.cpp)
if(RESINSIGHT_ENABLE_GRPC)
list(APPEND CODE_HEAD_FILES RiaGrpcConsoleApplication.h

View File

@@ -18,6 +18,7 @@
#include "RiaArgumentParser.h"
#include "RiaLogging.h"
#include "RiaMainTools.h"
#ifdef ENABLE_GRPC
#include "RiaGrpcConsoleApplication.h"
@@ -26,6 +27,7 @@
#include "RiaConsoleApplication.h"
#include "RiaGuiApplication.h"
#endif
#include "cvfProgramOptions.h"
#include "cvfqtUtils.h"
@@ -66,8 +68,12 @@ int main( int argc, char* argv[] )
return 1;
}
#endif
// Global initialization
RiaLogging::loggerInstance()->setLevel( int( RILogLevel::RI_LL_DEBUG ) );
// Create feature manager before the application object is created
RiaMainTools::initializeSingletons();
std::unique_ptr<RiaApplication> app( createApplication( argc, argv ) );
cvf::ProgramOptions progOpt;
@@ -112,6 +118,9 @@ int main( int argc, char* argv[] )
// Make sure project is closed to avoid assert and crash in destruction of widgets
app->closeProject();
app.reset();
RiaMainTools::releaseSingletonAndFactoryObjects();
return 0;
}
else if ( status == RiaApplication::ApplicationStatus::EXIT_WITH_ERROR )
@@ -147,6 +156,9 @@ int main( int argc, char* argv[] )
throw;
}
app.reset();
RiaMainTools::releaseSingletonAndFactoryObjects();
return exitCode;
}

View File

@@ -0,0 +1,61 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2022 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiaMainTools.h"
#include "RiaRegressionTestRunner.h"
#include "RiaSocketCommand.h"
#include "cafCmdFeature.h"
#include "cafCmdFeatureManager.h"
#include "cafPdmDefaultObjectFactory.h"
#include "cafPdmUiFieldEditorHandle.h"
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaMainTools::initializeSingletons()
{
caf::CmdFeatureManager::createSingleton();
RiaRegressionTestRunner::createSingleton();
caf::PdmDefaultObjectFactory::createSingleton();
}
//--------------------------------------------------------------------------------------------------
/// This method is used to release memory allocated by static functions. This enables use of memory allocation tools
/// after the application has closed down.
//--------------------------------------------------------------------------------------------------
void RiaMainTools::releaseSingletonAndFactoryObjects()
{
caf::CmdFeatureManager::deleteSingleton();
RiaRegressionTestRunner::deleteSingleton();
caf::PdmDefaultObjectFactory::deleteSingleton();
{
auto factory = caf::Factory<caf::PdmUiFieldEditorHandle, QString>::instance();
factory->deleteCreatorObjects();
}
{
auto factory = caf::Factory<caf::CmdFeature, std::string>::instance();
factory->deleteCreatorObjects();
}
{
auto factory = caf::Factory<RiaSocketCommand, QString>::instance();
factory->deleteCreatorObjects();
}
}

View File

@@ -0,0 +1,25 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2022 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
class RiaMainTools
{
public:
static void initializeSingletons();
static void releaseSingletonAndFactoryObjects();
};