Why am I leaking memory here (depth first search) c++? -


int solver::negamax(position* pos,int alpha,int beta, int color, int depth ) {   if(depth==0 || is_final(pos)){     return evaluate(pos);   }   else{     vector < position* > moves = generate_moves(pos->get_board());     vector < position* >::iterator move;     int min = 99999;     for(move = moves.begin(); move < moves.end(); move++){       int val = negamax(*move,alpha, beta, -color, depth - 1 );       if(val <= min){         min = val;         delete best;         best = null;         best = (*move)->get_board();        }       else{         delete *move; //so isnt cleaning up?         *move = null;       }      }      min = -min;      return min;      }   }  vector < position* > takeawaysolver::generate_moves(board *brd){   takeawayboard *board = static_cast<takeawayboard*>(brd);   vector < position* > moves;   if(board->get_data() >= 3){      takeawayboard *b = new takeawayboard(board->get_data() - 3);      position* p = new position(b);      moves.push_back(p);   }   if(board->get_data() >= 2){      takeawayboard *b = new takeawayboard(board->get_data() - 2);      position* p = new position(b);      moves.push_back(p);   }   takeawayboard *b = new takeawayboard(board->get_data() - 1);   position* p = new position(b);   moves.push_back(p);   return moves; 

}

i valgrinded program , i'm apparently leaking memory. seems i'm deleting unused objects, perhaps i'm not understanding something. generate_moves() allocate memory each of objects being pushed in. evaluate returns 1. seem possible i'm leaking memory in location?

you have if/else in *move deleted in 1 of paths. i'd check there.

for(move = moves.begin(); move < moves.end(); move++){           int val = negamax(*move,alpha, beta, -color, depth - 1 );           if(val <= min){             min = val;             delete best;             best = null;             best = (*move)->get_board();                                            //best deleted, *move not           }           else{             delete *move;             *move = null;           }          } 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -