mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#2873 New AsyncPointerVectorDeleter-classes to handle memory deletion in separate thread.
This commit is contained in:
parent
22c71b60dd
commit
743993718c
@ -30,6 +30,7 @@
|
|||||||
|
|
||||||
#include "RiuPlotMainWindow.h"
|
#include "RiuPlotMainWindow.h"
|
||||||
|
|
||||||
|
#include "cafAsyncObjectDeleter.h"
|
||||||
#include "cafSelectionManager.h"
|
#include "cafSelectionManager.h"
|
||||||
|
|
||||||
#include "cvfAssert.h"
|
#include "cvfAssert.h"
|
||||||
@ -50,7 +51,7 @@ void RicCloseSummaryCaseFeature::setupActionLook(QAction* actionToSetup)
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RicCloseSummaryCaseFeature::deleteSummaryCases(const std::vector<RimSummaryCase*>& cases)
|
void RicCloseSummaryCaseFeature::deleteSummaryCases(std::vector<RimSummaryCase*>& cases)
|
||||||
{
|
{
|
||||||
RimSummaryPlotCollection* summaryPlotColl = RiaSummaryTools::summaryPlotCollection();
|
RimSummaryPlotCollection* summaryPlotColl = RiaSummaryTools::summaryPlotCollection();
|
||||||
|
|
||||||
@ -66,13 +67,13 @@ void RicCloseSummaryCaseFeature::deleteSummaryCases(const std::vector<RimSummary
|
|||||||
summaryCase->firstAncestorOrThisOfTypeAsserted(summaryCaseMainCollection);
|
summaryCase->firstAncestorOrThisOfTypeAsserted(summaryCaseMainCollection);
|
||||||
|
|
||||||
summaryCaseMainCollection->removeCase(summaryCase);
|
summaryCaseMainCollection->removeCase(summaryCase);
|
||||||
delete summaryCase;
|
|
||||||
|
|
||||||
summaryCaseMainCollection->updateAllRequiredEditors();
|
summaryCaseMainCollection->updateAllRequiredEditors();
|
||||||
}
|
}
|
||||||
|
|
||||||
RiuPlotMainWindow* mainPlotWindow = RiaApplication::instance()->mainPlotWindow();
|
RiuPlotMainWindow* mainPlotWindow = RiaApplication::instance()->mainPlotWindow();
|
||||||
mainPlotWindow->updateSummaryPlotToolBar();
|
mainPlotWindow->updateSummaryPlotToolBar();
|
||||||
|
|
||||||
|
caf::AsyncRawPointerVectorDeleter<RimSummaryCase> summaryCaseDeleter(std::move(cases));
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
@ -32,7 +32,7 @@ class RicCloseSummaryCaseFeature : public caf::CmdFeature
|
|||||||
{
|
{
|
||||||
CAF_CMD_HEADER_INIT;
|
CAF_CMD_HEADER_INIT;
|
||||||
public:
|
public:
|
||||||
static void deleteSummaryCases(const std::vector<RimSummaryCase*>& cases);
|
static void deleteSummaryCases(std::vector<RimSummaryCase*>& cases);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// Overrides
|
// Overrides
|
||||||
|
@ -75,7 +75,8 @@ void RicCloseSummaryCaseInCollectionFeature::onActionTriggered(bool isChecked)
|
|||||||
|
|
||||||
if (summaryCaseMainCollections.size() > 0)
|
if (summaryCaseMainCollections.size() > 0)
|
||||||
{
|
{
|
||||||
RicCloseSummaryCaseFeature::deleteSummaryCases(summaryCaseMainCollections[0]->allSummaryCases());
|
std::vector<RimSummaryCase*> allSummaryCases = summaryCaseMainCollections[0]->allSummaryCases();
|
||||||
|
RicCloseSummaryCaseFeature::deleteSummaryCases(allSummaryCases);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<RimSummaryCaseCollection*> summaryCaseCollections;
|
std::vector<RimSummaryCaseCollection*> summaryCaseCollections;
|
||||||
@ -83,7 +84,8 @@ void RicCloseSummaryCaseInCollectionFeature::onActionTriggered(bool isChecked)
|
|||||||
|
|
||||||
for (RimSummaryCaseCollection* summaryCaseCollection : summaryCaseCollections)
|
for (RimSummaryCaseCollection* summaryCaseCollection : summaryCaseCollections)
|
||||||
{
|
{
|
||||||
RicCloseSummaryCaseFeature::deleteSummaryCases(summaryCaseCollection->allSummaryCases());
|
std::vector<RimSummaryCase*> collectionSummaryCases = summaryCaseCollection->allSummaryCases();
|
||||||
|
RicCloseSummaryCaseFeature::deleteSummaryCases(collectionSummaryCases);
|
||||||
}
|
}
|
||||||
|
|
||||||
RiuPlotMainWindow* mainPlotWindow = RiaApplication::instance()->mainPlotWindow();
|
RiuPlotMainWindow* mainPlotWindow = RiaApplication::instance()->mainPlotWindow();
|
||||||
|
@ -53,7 +53,7 @@ RimSummaryCaseCollection::RimSummaryCaseCollection()
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
RimSummaryCaseCollection::~RimSummaryCaseCollection()
|
RimSummaryCaseCollection::~RimSummaryCaseCollection()
|
||||||
{
|
{
|
||||||
m_cases.deleteAllChildObjects();
|
m_cases.deleteAllChildObjectsAsync();
|
||||||
updateReferringCurveSets();
|
updateReferringCurveSets();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,8 +75,8 @@ RimSummaryCaseMainCollection::RimSummaryCaseMainCollection()
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
RimSummaryCaseMainCollection::~RimSummaryCaseMainCollection()
|
RimSummaryCaseMainCollection::~RimSummaryCaseMainCollection()
|
||||||
{
|
{
|
||||||
m_cases.deleteAllChildObjects();
|
m_cases.deleteAllChildObjectsAsync();
|
||||||
m_caseCollections.deleteAllChildObjects();
|
m_caseCollections.deleteAllChildObjectsAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
@ -45,6 +45,9 @@ set( PROJECT_FILES
|
|||||||
cafTristate.h
|
cafTristate.h
|
||||||
cafFilePath.cpp
|
cafFilePath.cpp
|
||||||
cafFilePath.h
|
cafFilePath.h
|
||||||
|
cafAsyncObjectDeleter.h
|
||||||
|
cafAsyncObjectDeleter.inl
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
add_library( ${PROJECT_NAME}
|
add_library( ${PROJECT_NAME}
|
||||||
|
@ -0,0 +1,64 @@
|
|||||||
|
//##################################################################################################
|
||||||
|
//
|
||||||
|
// Custom Visualization Core library
|
||||||
|
// Copyright (C) 2011-2013 Ceetron AS
|
||||||
|
//
|
||||||
|
// This library may be used under the terms of either the GNU General Public License or
|
||||||
|
// the GNU Lesser General Public License as follows:
|
||||||
|
//
|
||||||
|
// GNU General Public License Usage
|
||||||
|
// This library 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.
|
||||||
|
//
|
||||||
|
// This library 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.
|
||||||
|
//
|
||||||
|
// GNU Lesser General Public License Usage
|
||||||
|
// This library is free software; you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation; either version 2.1 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This library 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 Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
|
||||||
|
// for more details.
|
||||||
|
//
|
||||||
|
//##################################################################################################
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace caf
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
class AsyncRawPointerVectorDeleter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
AsyncRawPointerVectorDeleter(std::vector<T*>&& pointerVector);
|
||||||
|
~AsyncRawPointerVectorDeleter();
|
||||||
|
void start();
|
||||||
|
private:
|
||||||
|
std::vector<T*> m_pointersToDelete;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class AsyncPdmPointerVectorDeleter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
AsyncPdmPointerVectorDeleter(std::vector<PdmPointer<T>>&& pdmPointerVector);
|
||||||
|
~AsyncPdmPointerVectorDeleter();
|
||||||
|
void start();
|
||||||
|
private:
|
||||||
|
std::vector<PdmPointer<T>> m_pointersToDelete;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "cafAsyncObjectDeleter.inl"
|
@ -0,0 +1,78 @@
|
|||||||
|
#include <thread>
|
||||||
|
|
||||||
|
namespace caf
|
||||||
|
{
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
/// Constructor that takes ownership of the data in the provided vector
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
template<typename T>
|
||||||
|
AsyncRawPointerVectorDeleter<T>::AsyncRawPointerVectorDeleter(std::vector<T*>&& pointerVector)
|
||||||
|
: m_pointersToDelete(std::move(pointerVector))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
/// Destructor will launch the asynchronous deletion if start() hasn't already been run.
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
template<typename T>
|
||||||
|
AsyncRawPointerVectorDeleter<T>::~AsyncRawPointerVectorDeleter()
|
||||||
|
{
|
||||||
|
if (!m_pointersToDelete.empty())
|
||||||
|
{
|
||||||
|
start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
/// Perform deletion of the pointers in a separate thread.
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
template<typename T>
|
||||||
|
void AsyncRawPointerVectorDeleter<T>::start()
|
||||||
|
{
|
||||||
|
std::thread([](std::vector<T*>&& pointerVector)
|
||||||
|
{
|
||||||
|
for (T* pointerToDelete : pointerVector)
|
||||||
|
{
|
||||||
|
delete pointerToDelete;
|
||||||
|
}
|
||||||
|
}, std::move(m_pointersToDelete)).detach();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
/// Constructor that takes ownership of the data in the provided vector
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
template<typename T>
|
||||||
|
AsyncPdmPointerVectorDeleter<T>::AsyncPdmPointerVectorDeleter(std::vector<PdmPointer<T>>&& pdmPointerVector)
|
||||||
|
: m_pointersToDelete(std::move(pdmPointerVector))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
/// Destructor will launch the asynchronous deletion if start() hasn't already been run.
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
template<typename T>
|
||||||
|
AsyncPdmPointerVectorDeleter<T>::~AsyncPdmPointerVectorDeleter()
|
||||||
|
{
|
||||||
|
if (!m_pointersToDelete.empty())
|
||||||
|
{
|
||||||
|
start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
/// Perform deletion of the pointers in a separate thread.
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
template<typename T>
|
||||||
|
void AsyncPdmPointerVectorDeleter<T>::start()
|
||||||
|
{
|
||||||
|
std::thread([](std::vector<PdmPointer<T>>&& pointerVector)
|
||||||
|
{
|
||||||
|
for (PdmPointer<T>& pointerToDelete : pointerVector)
|
||||||
|
{
|
||||||
|
delete pointerToDelete.rawPtr();
|
||||||
|
}
|
||||||
|
}, std::move(m_pointersToDelete)).detach();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -61,7 +61,8 @@ public:
|
|||||||
virtual size_t size() const { return m_pointers.size(); }
|
virtual size_t size() const { return m_pointers.size(); }
|
||||||
virtual bool empty() const { return m_pointers.empty(); }
|
virtual bool empty() const { return m_pointers.empty(); }
|
||||||
virtual void clear();
|
virtual void clear();
|
||||||
virtual void deleteAllChildObjects();
|
virtual void deleteAllChildObjects();
|
||||||
|
virtual void deleteAllChildObjectsAsync();
|
||||||
virtual void insertAt(int indexAfter, PdmObjectHandle* obj);
|
virtual void insertAt(int indexAfter, PdmObjectHandle* obj);
|
||||||
virtual PdmObjectHandle* at(size_t index);
|
virtual PdmObjectHandle* at(size_t index);
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
#include "cafAsyncObjectDeleter.h"
|
||||||
#include "cafClassTypeName.h"
|
#include "cafClassTypeName.h"
|
||||||
#include "cafPdmObjectHandle.h"
|
#include "cafPdmObjectHandle.h"
|
||||||
|
|
||||||
@ -138,6 +139,20 @@ void PdmChildArrayField<DataType*>::deleteAllChildObjects()
|
|||||||
m_pointers.clear();
|
m_pointers.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
/// Transfers ownership of the objects pointed to a separate thread.
|
||||||
|
/// Then clears the container and lets the thread delete the objects.
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
template<typename DataType>
|
||||||
|
void PdmChildArrayField<DataType*>::deleteAllChildObjectsAsync()
|
||||||
|
{
|
||||||
|
CAF_ASSERT(isInitializedByInitFieldMacro());
|
||||||
|
|
||||||
|
AsyncPdmPointerVectorDeleter<DataType> pointerDeleter(std::move(m_pointers));
|
||||||
|
CAF_ASSERT(m_pointers.empty()); // Object storage for m_pointers should be empty immediately.
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
/// Removes the pointer at index from the container. Does not delete the object pointed to.
|
/// Removes the pointer at index from the container. Does not delete the object pointed to.
|
||||||
/// Todo: This implementation can't be necessary in the new regime. Should be to just remove
|
/// Todo: This implementation can't be necessary in the new regime. Should be to just remove
|
||||||
|
Loading…
Reference in New Issue
Block a user