From 4782e82f02fa33e3840a27ddd3cb540f996b917a Mon Sep 17 00:00:00 2001 From: Antoine Regimbeau <antoine.regimbeau@c-s.fr> Date: Mon, 12 Mar 2018 11:15:49 +0100 Subject: [PATCH] ENH: add a free ressources method to release Data Object --- .../include/otbWrapperApplication.h | 4 + .../src/otbWrapperApplication.cxx | 92 +++++++++++++++++-- 2 files changed, 89 insertions(+), 7 deletions(-) diff --git a/Modules/Wrappers/ApplicationEngine/include/otbWrapperApplication.h b/Modules/Wrappers/ApplicationEngine/include/otbWrapperApplication.h index a05931d46b..1f7a60c933 100644 --- a/Modules/Wrappers/ApplicationEngine/include/otbWrapperApplication.h +++ b/Modules/Wrappers/ApplicationEngine/include/otbWrapperApplication.h @@ -850,6 +850,8 @@ public: void RegisterPipeline(); + void FreeRessources(); + protected: /** Constructor */ Application(); @@ -984,6 +986,8 @@ private: * implementation does nothing */ virtual void AfterExecuteAndWriteOutputs(); + virtual void DoFreeRessources(){}; + Application(const Application &); //purposely not implemented void operator =(const Application&); //purposely not implemented diff --git a/Modules/Wrappers/ApplicationEngine/src/otbWrapperApplication.cxx b/Modules/Wrappers/ApplicationEngine/src/otbWrapperApplication.cxx index 9a3c032c4a..ba2c32bb3d 100644 --- a/Modules/Wrappers/ApplicationEngine/src/otbWrapperApplication.cxx +++ b/Modules/Wrappers/ApplicationEngine/src/otbWrapperApplication.cxx @@ -441,7 +441,6 @@ Application::RegisterPipeline() Parameter * param = GetParameterByKey(key); InputVectorDataParameter * inP = dynamic_cast< InputVectorDataParameter * > ( param ); - std::cout<<"Getting data from outputvectordataparameter"<<std::endl; if ( !inP->HasValue() ) continue; VectorDataType * inData = inP->GetVectorData(); @@ -508,6 +507,90 @@ Application::RegisterPipeline() } } +void Application::FreeRessources() +{ + std::set< itk::DataObject * > dataSet; + std::vector<std::string> paramList = GetParametersKeys(true); + for (std::vector<std::string>::const_iterator it = paramList.begin(); + it != paramList.end(); + ++it) + { + std::string key = *it; + if ( GetParameterType(key) == ParameterType_OutputImage ) + { + Parameter* param = GetParameterByKey(key); + OutputImageParameter * outP = dynamic_cast<OutputImageParameter*>(param); + itk::ImageBase<2> * outData = outP->GetValue(); + if ( outData ) + dataSet.insert(outData); + } + else if ( GetParameterType(key) == ParameterType_OutputVectorData ) + { + Parameter* param = GetParameterByKey(key); + OutputVectorDataParameter * outP = dynamic_cast<OutputVectorDataParameter*>(param); + Wrapper::VectorDataType * outData = outP->GetValue(); + if ( outData ) + dataSet.insert(outData); + } + else + continue; + } + // DFS + std::stack< itk::ProcessObject * > processStack; + for ( auto data : dataSet ) + { + auto process = (data->GetSource()).GetPointer(); + if ( process ) + processStack.push( process ); + } + + while ( !processStack.empty() ) + { + itk::ProcessObject * current = processStack.top(); + processStack.pop(); + std::cout<<current->GetNameOfClass()<<std::endl; + if ( !current ) + continue; + auto inputVector = current->GetInputs(); + for ( auto data : inputVector ) + { + if ( !data.GetPointer() || dataSet.count( data.GetPointer() ) ) + continue; + if ( dynamic_cast< ObjectListInterface *> (data.GetPointer()) ) + { + ObjectListInterface * list = + dynamic_cast< ObjectListInterface *> (data.GetPointer()); + int length = list->Size(); + for ( int i = 0 ; i < length ; i++ ) + { + itk::DataObject * newData = list->GetNthDataObject(i); + if ( !newData || dataSet.count( newData ) ) + continue; + dataSet.insert( newData ); + itk::ProcessObject * process = newData->GetSource().GetPointer(); + if ( process ) + processStack.push( process ); + } + } + else + { + dataSet.insert( data.GetPointer() ); + itk::ProcessObject * process = data->GetSource().GetPointer(); + if ( process ) + processStack.push( process ); + } + } + } + + for ( auto data : dataSet ) + { + std::cout<<"one bulk is freed"<<std::endl; + data->ReleaseData(); + } + + DoFreeRessources(); +} + int Application::Execute() { @@ -672,12 +755,7 @@ int Application::ExecuteAndWriteOutput() this->AfterExecuteAndWriteOutputs(); m_Chrono.Stop(); - // for ( auto filter : m_Filters ) - // { - // std::cout<<"For filter : "<<filter->GetNameOfClass()<< - // " count : "<<filter->GetReferenceCount()<<std::endl; - // } - + FreeRessources(); m_Filters.clear(); return status; } -- GitLab