From 59027b4e24c5f69b89f7ff8c05bb1416310cd00b Mon Sep 17 00:00:00 2001 From: jboisson Date: Fri, 7 Mar 2008 16:00:58 +0000 Subject: [PATCH] moHC and moTS have been updated to avoid memory leaks... must be tested under windows git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1101 331e1502-861f-0410-8da2-ba01fb791d7f --- trunk/paradiseo-mo/src/moHC.h | 22 +++++++++++++++++----- trunk/paradiseo-mo/src/moTS.h | 22 +++++++++++++++++----- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/trunk/paradiseo-mo/src/moHC.h b/trunk/paradiseo-mo/src/moHC.h index ed77f7048..a945c223f 100755 --- a/trunk/paradiseo-mo/src/moHC.h +++ b/trunk/paradiseo-mo/src/moHC.h @@ -68,8 +68,8 @@ class moHC:public moAlgo < typename M::EOType > */ moHC (moMoveInit < M > & _move_initializer, moNextMove < M > & _next_move_generator, moMoveIncrEval < M > & _incremental_evaluation, moMoveSelect < M > & _move_selection, eoEvalFunc < EOT > & _full_evaluation) : - move_explorer ( *new moHCMoveLoopExpl < M > (_move_initializer, _next_move_generator, _incremental_evaluation, _move_selection) ), - full_evaluation (_full_evaluation) + move_explorer(new moHCMoveLoopExpl(_move_initializer, _next_move_generator, _incremental_evaluation, _move_selection)), + full_evaluation (_full_evaluation), move_explorer_memory_allocation(true) {} //! Light constructor. @@ -80,9 +80,18 @@ class moHC:public moAlgo < typename M::EOType > \param _full_evaluation a full evaluation function. */ moHC (moMoveExpl < M > & _move_explorer, eoEvalFunc < EOT > & _full_evaluation): - move_explorer (_move_explorer), full_evaluation (_full_evaluation) + move_explorer (_move_explorer), full_evaluation (_full_evaluation), move_explorer_memory_allocation(false) {} + //! Destructor + ~moHC() + { + if(move_explorer_memory_allocation) + { + delete(move_explorer); + } + } + //! Function which launches the HC /*! The HC has to improve a current solution. @@ -105,7 +114,7 @@ class moHC:public moAlgo < typename M::EOType > do { _solution=new_solution; - move_explorer (_solution, new_solution); + (*move_explorer) (_solution, new_solution); } while ( new_solution.fitness() > _solution.fitness() ); @@ -115,10 +124,13 @@ class moHC:public moAlgo < typename M::EOType > private: //! Complete exploration of the neighborhood. - moMoveExpl < M > & move_explorer; + moMoveExpl < M > * move_explorer; //! A full evaluation function. eoEvalFunc < EOT > & full_evaluation; + + //! Indicate if the memory has been allocated for the move_explorer. + bool move_explorer_memory_allocation; }; #endif diff --git a/trunk/paradiseo-mo/src/moTS.h b/trunk/paradiseo-mo/src/moTS.h index e205151b5..455c542e3 100755 --- a/trunk/paradiseo-mo/src/moTS.h +++ b/trunk/paradiseo-mo/src/moTS.h @@ -73,9 +73,9 @@ class moTS:public moAlgo < typename M::EOType > moMoveIncrEval < M > & _incremental_evaluation, moTabuList < M > & _tabu_list, moAspirCrit < M > & _aspiration_criterion, moSolContinue < EOT > & _continue, eoEvalFunc < EOT > & _full_evaluation): - move_explorer ( *new moTSMoveLoopExpl < M >(_move_initializer, _next_move_generator, _incremental_evaluation, + move_explorer (new moTSMoveLoopExpl < M >(_move_initializer, _next_move_generator, _incremental_evaluation, _tabu_list,_aspiration_criterion) ), - continu (_continue), full_evaluation (_full_evaluation) + continu (_continue), full_evaluation (_full_evaluation), move_explorer_memory_allocation(true) {} //! Constructor with less parameters @@ -87,8 +87,17 @@ class moTS:public moAlgo < typename M::EOType > \param _full_evaluation A full evaluation function. */ moTS (moMoveExpl < M > & _move_explorer, moSolContinue < EOT > & _continue, eoEvalFunc < EOT > & _full_evaluation): - move_explorer (_move_explorer), continu (_continue), full_evaluation (_full_evaluation) + move_explorer (_move_explorer), continu (_continue), full_evaluation (_full_evaluation), move_explorer_memory_allocation(false) {} + + //! Destructor + ~moTS() + { + if(move_explorer_memory_allocation) + { + delete(move_explorer); + } + } //! Function which launchs the Tabu Search /*! @@ -119,7 +128,7 @@ class moTS:public moAlgo < typename M::EOType > do { - move_explorer (_solution, new_solution); + (*move_explorer) (_solution, new_solution); // Updating the best solution found until now ? if (new_solution.fitness() > _solution.fitness()) @@ -139,13 +148,16 @@ class moTS:public moAlgo < typename M::EOType > private: //! Neighborhood explorer - moMoveExpl < M > & move_explorer; + moMoveExpl < M > * move_explorer; //! Stop criterion moSolContinue < EOT > & continu; //! Full evaluation function eoEvalFunc < EOT > & full_evaluation; + + //! Indicate if the memory has been allocated for the move_explorer. + bool move_explorer_memory_allocation; }; #endif