From d5a936694d72caaa62aed56872d2045ac2ed8578 Mon Sep 17 00:00:00 2001 From: eap Date: Fri, 14 Dec 2012 12:13:58 +0000 Subject: [PATCH] 21948: EDF SMESH : Memory is not freed when deleting a mesh Define SALOME::GenericObj_wrap< GENOBJ > + SALOME_GenericObj_wrap.hxx --- src/GenericObj/CMakeLists.txt | 2 +- src/GenericObj/Makefile.am | 5 +- src/GenericObj/SALOME_GenericObj_wrap.hxx | 180 ++++++++++++++++++++++ 3 files changed, 184 insertions(+), 3 deletions(-) create mode 100644 src/GenericObj/SALOME_GenericObj_wrap.hxx diff --git a/src/GenericObj/CMakeLists.txt b/src/GenericObj/CMakeLists.txt index 203be8e04..423d1eb39 100755 --- a/src/GenericObj/CMakeLists.txt +++ b/src/GenericObj/CMakeLists.txt @@ -31,5 +31,5 @@ SET_TARGET_PROPERTIES(SalomeGenericObj PROPERTIES COMPILE_FLAGS "${OMNIORB_DEFIN TARGET_LINK_LIBRARIES(SalomeGenericObj SALOMELocalTrace SalomeIDLKernel) INSTALL(TARGETS SalomeGenericObj DESTINATION ${KERNEL_salomelib_LIBS}) -INSTALL(FILES SALOME_GenericObj_i.hh DESTINATION ${KERNEL_salomeinclude_HEADERS}) +INSTALL(FILES SALOME_GenericObj_i.hh SALOME_GenericObj_wrap.hxx DESTINATION ${KERNEL_salomeinclude_HEADERS}) diff --git a/src/GenericObj/Makefile.am b/src/GenericObj/Makefile.am index 438bdea2a..921a75866 100644 --- a/src/GenericObj/Makefile.am +++ b/src/GenericObj/Makefile.am @@ -29,7 +29,8 @@ include $(top_srcdir)/salome_adm/unix/make_common_starter.am # header files salomeinclude_HEADERS =\ - SALOME_GenericObj_i.hh + SALOME_GenericObj_i.hh \ + SALOME_GenericObj_wrap.hxx # Libraries targets lib_LTLIBRARIES = libSalomeGenericObj.la @@ -42,6 +43,6 @@ libSalomeGenericObj_la_CPPFLAGS = \ -I$(srcdir)/../SALOMELocalTrace \ -I$(top_builddir)/idl \ @CORBA_CXXFLAGS@ @CORBA_INCLUDES@ -libSalomeGenericObj_la_LIBADD = \ + libSalomeGenericObj_la_LIBADD = \ ../SALOMELocalTrace/libSALOMELocalTrace.la \ $(top_builddir)/idl/libSalomeIDLKernel.la diff --git a/src/GenericObj/SALOME_GenericObj_wrap.hxx b/src/GenericObj/SALOME_GenericObj_wrap.hxx new file mode 100644 index 000000000..594f8c7c5 --- /dev/null +++ b/src/GenericObj/SALOME_GenericObj_wrap.hxx @@ -0,0 +1,180 @@ +// Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// 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. +// +// 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 for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// File : SALOME_GenericObj_wrap.hxx +// Created : Fri Dec 7 11:32:45 2012 +// Author : Edward AGAPOV (eap) + + +#ifndef __SALOME_GenericObj_wrap_HXX__ +#define __SALOME_GenericObj_wrap_HXX__ + +namespace SALOME +{ + // call UnRegister on each element in a sequence of SALOME::GenericObj's + template< class SEQ_OF_GENOBJ > + void UnRegister( SEQ_OF_GENOBJ& seq ) + { + for ( size_t i = 0; i < seq->length(); ++i ) + if ( !CORBA::is_nil( seq[i] )) + seq[i]->UnRegister(); + } + + // This class is intended to free the user of objects, iherited from SALOME::GenericObj, + // from a need to call UnRegister() after having finished using the object. + // The behavior of this class is similar to that of var CORBA class and thus it + // can replace var types of wrapped CORBA classes in some code like follows: + // + // namespace SALOMEDS + // { + // typedef SALOME::GenericObj_wrap< SObject > SObject_wrap; + // typedef SALOME::GenericObj_wrap< ChildIterator > ChildIterator_wrap; + // } + // ... + // SALOMEDS::ChildIterator_wrap anIter = aStudy->NewChildIterator( mainSO ); + // for ( ; anIter->More(); anIter->Next() ) { + // SALOMEDS::SObject_wrap so = anIter->Value(); + // ... + // + // Note that the need to call UnRegister(), and thus to use GenericObj_wrap, depends on + // implementation of a method returning a GenericObj servant. If the method creates a + // servant, than UnRegister() must be called. If the method retrieves a servant from + // some storage, then it's wrong to call UnRegister(). + // + // A pleasant additional feature privided by this class is that one can assign + // GenericObj_wrap< BASE_CLASS > to GenericObj_wrap< CHILD_CLASS > without + // calling CHILD_CLASS::_narrow(). + // + template< class GENOBJ > + class GenericObj_wrap + { + public: + typedef typename GENOBJ::_ptr_type TPtr; + typedef typename GENOBJ::_var_type TVar; + + // CONSTRUCTION + GenericObj_wrap() + { + _isOwn = false; + } + GenericObj_wrap( const TPtr& obj ) + : _obj( obj ), _isOwn( true ) + {} + GenericObj_wrap( const TVar& obj ) + : _obj( obj ), _isOwn( true ) + {} + GenericObj_wrap( const GenericObj_wrap& wrap ) + : _obj( wrap.in() ), _isOwn( wrap._getIsOwn() ) + { + _register(); + } + template< class BASE_GENOBJ > + GenericObj_wrap( const GenericObj_wrap& wrap ) + : _obj( GENOBJ::_narrow( wrap.in() )), _isOwn( wrap._getIsOwn() ) + { + _register(); + } + // DESTRUCTION + ~GenericObj_wrap() + { + _release(); + } + // COPYING + GenericObj_wrap& operator=( const TPtr& obj ) + { + _release(); + _obj = obj; + _isOwn = true; + return *this; + } + GenericObj_wrap& operator=( const TVar& obj ) + { + _release(); + _obj = obj; + _isOwn = true; + return *this; + } + GenericObj_wrap& operator=( const GenericObj_wrap& wrap ) + { + _release(); + _obj = wrap.in(); + _isOwn = wrap._getIsOwn(); + _register(); + return *this; + } + template< class BASE_GENOBJ > + GenericObj_wrap& operator=( const GenericObj_wrap& wrap ) + { + _release(); + _obj = GENOBJ::_narrow( wrap.in() ); + _isOwn = wrap._getIsOwn(); + _register(); + return *this; + } + // ACCESS + operator TPtr() + { + return _obj.in(); + } + operator TVar() + { + return _obj; + } + TVar operator->() + { + return _obj; + } + TPtr in() const + { + return _obj; + } + TPtr& inout() + { + _release(); // returned reference to _obj will be initialized with another object + _isOwn = true; + return _obj.inout(); + } + TPtr _retn() + { + _isOwn = false; + return _obj._retn(); + } + + bool _getIsOwn() const { return _isOwn; } + + private: + void _register() + { + if ( _isOwn && !CORBA::is_nil( _obj )) + _obj->Register(); + } + void _release() + { + if ( _isOwn && !CORBA::is_nil( _obj )) + _obj->UnRegister(); + _isOwn = false; + } + TVar _obj; + bool _isOwn; + }; +} // namespace SALOME + +#endif -- 2.39.2