From c17778cc2fb90a5e9b91b93e28f491dde49dd112 Mon Sep 17 00:00:00 2001 From: caremoli Date: Wed, 16 Sep 2009 13:18:51 +0000 Subject: [PATCH] CCAR: remove more memory leaks in SALOMEDS --- src/SALOMEDS/SALOMEDS_StudyManager_i.cxx | 43 +++++++++++-------- src/SALOMEDS/SALOMEDS_Study_i.cxx | 30 ++++++++++++- src/SALOMEDS/SALOMEDS_Study_i.hxx | 2 + .../SALOMEDSImpl_AttributeIOR.cxx | 2 +- src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx | 9 ++++ src/SALOMEDSImpl/SALOMEDSImpl_Study.hxx | 1 + .../SALOMEDSImpl_StudyBuilder.cxx | 4 ++ 7 files changed, 72 insertions(+), 19 deletions(-) diff --git a/src/SALOMEDS/SALOMEDS_StudyManager_i.cxx b/src/SALOMEDS/SALOMEDS_StudyManager_i.cxx index f2759d888..0f2f93ac6 100644 --- a/src/SALOMEDS/SALOMEDS_StudyManager_i.cxx +++ b/src/SALOMEDS/SALOMEDS_StudyManager_i.cxx @@ -122,7 +122,8 @@ SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::NewStudy(const char* study_name) MESSAGE("NewStudy : Creating the CORBA servant holding it... "); - SALOMEDS_Study_i *Study_servant = new SALOMEDS_Study_i(aStudyImpl, _orb); + SALOMEDS_Study_i *Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb); + PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa registered in _mapOfPOA SALOMEDS::Study_var Study = Study_servant->_this(); // Register study in the naming service @@ -163,8 +164,9 @@ SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::Open(const char* aUrl) MESSAGE("Open : Creating the CORBA servant holding it... "); // Temporary aStudyUrl in place of study name - SALOMEDS_Study_i * Study_servant = new SALOMEDS_Study_i(aStudyImpl, _orb); - SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(Study_servant->_this()); + SALOMEDS_Study_i * Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb); + PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa register in _mapOfPOA + SALOMEDS::Study_var Study = Study_servant->_this(); // Assign the value of the IOR in the study->root CORBA::String_var IORStudy = _orb->object_to_string(Study); @@ -175,7 +177,7 @@ SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::Open(const char* aUrl) if(!_name_service->Change_Directory("/Study")) MESSAGE( "Unable to access the study directory" ) else _name_service->Register(Study, CORBA::string_dup(aStudyImpl->Name().c_str())); - return Study; + return Study._retn(); } @@ -202,6 +204,14 @@ void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy) SALOMEDS::unlock(); aStudy->Close(); SALOMEDS::lock(); + + //remove study servant + PortableServer::POA_ptr poa=GetPOA(aStudy); + PortableServer::ServantBase* aservant=poa->reference_to_servant(aStudy); + PortableServer::ObjectId_var anObjectId = poa->servant_to_id(aservant); + poa->deactivate_object(anObjectId.in()); + aservant->_remove_ref(); // decrement for the call to reference_to_servant + aservant->_remove_ref(); // to delete the object } //============================================================================ @@ -313,10 +323,8 @@ SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyNa return SALOMEDS::Study::_nil(); } - SALOMEDS_Study_i* aStudy_servant = new SALOMEDS_Study_i(aStudyImpl, _orb); - SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(aStudy_servant->_this()); - - return aStudy._retn(); + SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb); + return aStudy_servant->_this(); } //============================================================================ @@ -336,11 +344,8 @@ SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID) return SALOMEDS::Study::_nil(); } - SALOMEDS_Study_i* aStudy_servant = new SALOMEDS_Study_i(aStudyImpl, _orb); - CORBA::Object_var obj = aStudy_servant->_this(); - SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(obj); - - return aStudy._retn(); + SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb); + return aStudy_servant->_this(); } @@ -355,7 +360,8 @@ CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject) SALOMEDS::Study_var aStudy = theObject->GetStudy(); SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId()); - SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID()); + CORBA::String_var anID = theObject->GetID(); + SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in()); SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb); bool ret = _impl->CanCopy(anObject, aDriver); @@ -374,7 +380,8 @@ CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject) SALOMEDS::Study_var aStudy = theObject->GetStudy(); SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId()); - SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID()); + CORBA::String_var anID = theObject->GetID(); + SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in()); SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb); bool ret = _impl->Copy(anObject, aDriver); @@ -393,7 +400,8 @@ CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject SALOMEDS::Study_var aStudy = theObject->GetStudy(); SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId()); - SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID()); + CORBA::String_var anID = theObject->GetID(); + SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in()); SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb); bool ret = _impl->CanPaste(anObject, aDriver); @@ -415,7 +423,8 @@ SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theOb SALOMEDS::Study_var aStudy = theObject->GetStudy(); SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId()); - SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID()); + CORBA::String_var anID = theObject->GetID(); + SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in()); SALOMEDSImpl_SObject aNewSO; try { diff --git a/src/SALOMEDS/SALOMEDS_Study_i.cxx b/src/SALOMEDS/SALOMEDS_Study_i.cxx index fb0e28abd..c6127cc21 100644 --- a/src/SALOMEDS/SALOMEDS_Study_i.cxx +++ b/src/SALOMEDS/SALOMEDS_Study_i.cxx @@ -25,6 +25,7 @@ // #include "utilities.h" #include "SALOMEDS_Study_i.hxx" +#include "SALOMEDS_StudyManager_i.hxx" #include "SALOMEDS_UseCaseIterator_i.hxx" #include "SALOMEDS_GenericAttribute_i.hxx" #include "SALOMEDS_AttributeStudyProperties_i.hxx" @@ -55,6 +56,8 @@ using namespace std; +std::map SALOMEDS_Study_i::_mapOfStudies; + //============================================================================ /*! Function : SALOMEDS_Study_i * Purpose : SALOMEDS_Study_i constructor @@ -76,6 +79,13 @@ SALOMEDS_Study_i::SALOMEDS_Study_i(SALOMEDSImpl_Study* theImpl, //============================================================================ SALOMEDS_Study_i::~SALOMEDS_Study_i() { + //delete the builder servant + PortableServer::POA_var poa=_builder->_default_POA(); + PortableServer::ObjectId_var anObjectId = poa->servant_to_id(_builder); + poa->deactivate_object(anObjectId.in()); + _builder->_remove_ref(); + //delete implementation + delete _impl; } //============================================================================ @@ -421,7 +431,8 @@ SALOMEDS::ChildIterator_ptr SALOMEDS_Study_i::NewChildIterator(SALOMEDS::SObject { SALOMEDS::Locker lock; - SALOMEDSImpl_SObject aSO = _impl->GetSObject(theSO->GetID()); + CORBA::String_var anID=theSO->GetID(); + SALOMEDSImpl_SObject aSO = _impl->GetSObject(anID.in()); SALOMEDSImpl_ChildIterator anItr(aSO); //Create iterator @@ -581,6 +592,18 @@ SALOMEDS::Study_ptr SALOMEDS_Study_i::GetStudy(const DF_Label& theLabel, CORBA:: return SALOMEDS::Study::_nil(); } +SALOMEDS_Study_i* SALOMEDS_Study_i::GetStudyServant(SALOMEDSImpl_Study* aStudyImpl, CORBA::ORB_ptr orb) +{ + if (_mapOfStudies.find(aStudyImpl) != _mapOfStudies.end()) + return _mapOfStudies[aStudyImpl]; + else + { + SALOMEDS_Study_i *Study_servant = new SALOMEDS_Study_i(aStudyImpl, orb); + _mapOfStudies[aStudyImpl]=Study_servant; + return Study_servant; + } +} + void SALOMEDS_Study_i::IORUpdated(SALOMEDSImpl_AttributeIOR* theAttribute) { SALOMEDS::Locker lock; @@ -685,8 +708,13 @@ void SALOMEDS_Study_i::Close() catch (CORBA::Exception&) {/*pass*/ } } + sco->Destroy(); } + //Does not need any more this iterator + itcomponent->Destroy(); + + _impl->Close(); } diff --git a/src/SALOMEDS/SALOMEDS_Study_i.hxx b/src/SALOMEDS/SALOMEDS_Study_i.hxx index 52ada453c..d53e660d6 100644 --- a/src/SALOMEDS/SALOMEDS_Study_i.hxx +++ b/src/SALOMEDS/SALOMEDS_Study_i.hxx @@ -51,6 +51,7 @@ private: CORBA::ORB_var _orb; SALOMEDSImpl_Study* _impl; SALOMEDS_StudyBuilder_i* _builder; + static std::map _mapOfStudies; public: @@ -244,6 +245,7 @@ public: virtual void StudyId(CORBA::Short id); static SALOMEDS::Study_ptr GetStudy(const DF_Label& theLabel, CORBA::ORB_ptr orb); + static SALOMEDS_Study_i* GetStudyServant(SALOMEDSImpl_Study*, CORBA::ORB_ptr orb); static void IORUpdated(SALOMEDSImpl_AttributeIOR* theAttribute); diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_AttributeIOR.cxx b/src/SALOMEDSImpl/SALOMEDSImpl_AttributeIOR.cxx index c70ac8ed5..588c83d32 100644 --- a/src/SALOMEDSImpl/SALOMEDSImpl_AttributeIOR.cxx +++ b/src/SALOMEDSImpl/SALOMEDSImpl_AttributeIOR.cxx @@ -56,7 +56,6 @@ SALOMEDSImpl_AttributeIOR* SALOMEDSImpl_AttributeIOR::Set (const DF_Label& L, } A->SetValue(S); - SALOMEDSImpl_Study::IORUpdated(A); return A; } @@ -73,6 +72,7 @@ void SALOMEDSImpl_AttributeIOR::SetValue(const std::string& theValue) Backup(); myString = theValue; + //add IOR entry in study SALOMEDSImpl_Study::IORUpdated(this); } diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx b/src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx index 7d40c15db..edc77e49f 100644 --- a/src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx +++ b/src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx @@ -965,6 +965,15 @@ void SALOMEDSImpl_Study::UpdateIORLabelMap(const string& anIOR,const string& anE myIORLabels[anIOR] = aLabel; } +void SALOMEDSImpl_Study::DeleteIORLabelMapItem(const std::string& anIOR) +{ + if (myIORLabels.find(anIOR) != myIORLabels.end()) + { + //remove the ior entry + myIORLabels.erase(anIOR); + } +} + SALOMEDSImpl_Study* SALOMEDSImpl_Study::GetStudy(const DF_Label& theLabel) { SALOMEDSImpl_StudyHandle* Att; diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_Study.hxx b/src/SALOMEDSImpl/SALOMEDSImpl_Study.hxx index 8e29584c3..55a2bca77 100644 --- a/src/SALOMEDSImpl/SALOMEDSImpl_Study.hxx +++ b/src/SALOMEDSImpl/SALOMEDSImpl_Study.hxx @@ -196,6 +196,7 @@ public: virtual void StudyId(int id); + virtual void DeleteIORLabelMapItem(const std::string& anIOR); virtual void UpdateIORLabelMap(const std::string& anIOR, const std::string& aLabel); virtual std::vector FindDependances(const SALOMEDSImpl_SObject& anObject); diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_StudyBuilder.cxx b/src/SALOMEDSImpl/SALOMEDSImpl_StudyBuilder.cxx index 7edbea023..4ec0d3b39 100644 --- a/src/SALOMEDSImpl/SALOMEDSImpl_StudyBuilder.cxx +++ b/src/SALOMEDSImpl/SALOMEDSImpl_StudyBuilder.cxx @@ -205,6 +205,7 @@ bool SALOMEDSImpl_StudyBuilder::RemoveObject(const SALOMEDSImpl_SObject& anObjec SALOMEDSImpl_AttributeIOR* anAttr = NULL; //Remove from IORLabel map if ((anAttr=(SALOMEDSImpl_AttributeIOR*)Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) { + _study->DeleteIORLabelMapItem(anAttr->Value()); } Lab.ForgetAllAttributes(); @@ -240,6 +241,7 @@ bool SALOMEDSImpl_StudyBuilder::RemoveObjectWithChildren(const SALOMEDSImpl_SObj } SALOMEDSImpl_AttributeIOR* anAttr = NULL; //Remove from IORLabel map if ((anAttr=(SALOMEDSImpl_AttributeIOR*)Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) { + _study->DeleteIORLabelMapItem(anAttr->Value()); } DF_ChildIterator it(Lab, true); @@ -252,6 +254,7 @@ bool SALOMEDSImpl_StudyBuilder::RemoveObjectWithChildren(const SALOMEDSImpl_SObj } SALOMEDSImpl_AttributeIOR* anAttr = NULL; //Remove from IORLabel map if ((anAttr=(SALOMEDSImpl_AttributeIOR*)aLabel.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) { + _study->DeleteIORLabelMapItem(anAttr->Value()); } } @@ -533,6 +536,7 @@ bool SALOMEDSImpl_StudyBuilder::RemoveAttribute(const SALOMEDSImpl_SObject& anOb if (aTypeOfAttribute == string("AttributeIOR")) { // Remove from IORLabel map SALOMEDSImpl_AttributeIOR* anAttr = NULL; if ((anAttr=(SALOMEDSImpl_AttributeIOR*)Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) { + _study->DeleteIORLabelMapItem(anAttr->Value()); } } -- 2.39.2