title: Memory leakage in deleting nested pointers in C++ in Visual Studio
body: <p>Hi I have a class it contains some arrays of structures that are defined in header as following:</p>
  
  <pre><code>struct ball
  {
      int* time;
      float* x;
      float* y;
      float* vx;
      float* vy;
  }*Ball;
  
  struct team
  {
      int* time;
      char* side;
      int* unum;
      int* type;
      int* state;
      float* x;
      float* y;
      float* vx;
      float* vy;
      float* body_angle;
      float* head_angle;
  }**TeamLeft, **TeamRight;   
  
  struct teamscore
  {       
      string name_l;
      string name_r;
      int* time;
      int* score_l;
      int* score_r;
      int* pen_score_l;
      int* pen_score_r;
      int* pen_miss_l;
      int* pen_miss_r;
  }*TeamScore;
  
  int** playmode;
  </code></pre>
  
  <p>In constructor memory allocation is performed as the following and then the structures are filled desired initial values:</p>
  
  <pre><code>Ball = new ball[file_num];
  TeamScore = new teamscore[file_num];
  TeamLeft = new team*[file_num];
  TeamRight = new team*[file_num];
  playmode = new int*[file_num];
  
  for (int i = 0; i &lt; file_num; i++)
  {
      Ball[i].time = new int[frames_num];
      Ball[i].x = new float[frames_num];
      Ball[i].y = new float[frames_num];
      Ball[i].vx = new float[frames_num];
      Ball[i].vy = new float[frames_num];
  
      TeamLeft[i] = new team[11];
      TeamRight[i] = new team[11];
  
      TeamScore[i].time = new int[frames_num];
      TeamScore[i].score_l = new int[frames_num];
      TeamScore[i].score_r = new int[frames_num];
      TeamScore[i].pen_score_l = new int[frames_num];
      TeamScore[i].pen_score_r = new int[frames_num];
      TeamScore[i].pen_miss_l = new int[frames_num];
      TeamScore[i].pen_miss_r = new int[frames_num];
  
      playmode[i] = new int[frames_num];
  
      for (int j = 0; j &lt; 11; j++)
      {
          TeamLeft[i][j].time = new int[frames_num];
          TeamLeft[i][j].side = new char[frames_num];
          TeamLeft[i][j].unum = new int[frames_num];
          TeamLeft[i][j].type = new int[frames_num];
          TeamLeft[i][j].state = new int[frames_num];
          TeamLeft[i][j].x = new float[frames_num];
          TeamLeft[i][j].y = new float[frames_num];
          TeamLeft[i][j].vx = new float[frames_num];
          TeamLeft[i][j].vy = new float[frames_num];
          TeamLeft[i][j].body_angle = new float[frames_num];
          TeamLeft[i][j].head_angle = new float[frames_num];
  
          TeamRight[i][j].time = new int[frames_num];
          TeamRight[i][j].side = new char[frames_num];
          TeamRight[i][j].unum = new int[frames_num];
          TeamRight[i][j].type = new int[frames_num];
          TeamRight[i][j].state = new int[frames_num];
          TeamRight[i][j].x = new float[frames_num];
          TeamRight[i][j].y = new float[frames_num];
          TeamRight[i][j].vx = new float[frames_num];
          TeamRight[i][j].vy = new float[frames_num];
          TeamRight[i][j].body_angle = new float[frames_num];
          TeamRight[i][j].head_angle = new float[frames_num];
      }
  }
  
  for (int Fi = 0; Fi &lt; file_num; Fi++)
  {
      TeamScore[Fi].name_l = "None";
      TeamScore[Fi].name_r = "None";
      for (int Fr = 0; Fr &lt; frames_num; Fr++)
      {
  
          Ball[Fi].time[Fr] = 0;
          Ball[Fi].x[Fr] = 0;
          Ball[Fi].y[Fr] = 0;
          Ball[Fi].vx[Fr] = 0;
          Ball[Fi].vy[Fr] = 0;
  
          TeamScore[Fi].time[Fr] = 0;
          TeamScore[Fi].score_l[Fr] = 0;
          TeamScore[Fi].score_r[Fr] = 0;
          TeamScore[Fi].pen_score_l[Fr] = 0;
          TeamScore[Fi].pen_score_r[Fr] = 0;
          TeamScore[Fi].pen_miss_l[Fr] = 0;
          TeamScore[Fi].pen_miss_r[Fr] = 0;
  
          playmode[Fi][Fr] = 0;           
  
          for (int Ti = 0; Ti &lt; 11; Ti++)
          {
              TeamLeft[Fi][Ti].time[Fr] = 0;
              TeamLeft[Fi][Ti].side[Fr] = 'L';
              TeamLeft[Fi][Ti].unum[Fr] = 0;
              TeamLeft[Fi][Ti].type[Fr] = 0;
              TeamLeft[Fi][Ti].state[Fr] = 0;
              TeamLeft[Fi][Ti].x[Fr] = 0;
              TeamLeft[Fi][Ti].y[Fr] = 0;
              TeamLeft[Fi][Ti].vx[Fr] = 0;
              TeamLeft[Fi][Ti].vy[Fr] = 0;
              TeamLeft[Fi][Ti].body_angle[Fr] = 0;
              TeamLeft[Fi][Ti].head_angle[Fr] = 0;
  
              TeamRight[Fi][Ti].time[Fr] = 0;
              TeamRight[Fi][Ti].side[Fr] = 'R';
              TeamRight[Fi][Ti].unum[Fr] = 0;
              TeamRight[Fi][Ti].type[Fr] = 0;
              TeamRight[Fi][Ti].state[Fr] = 0;
              TeamRight[Fi][Ti].x[Fr] = 0;
              TeamRight[Fi][Ti].y[Fr] = 0;
              TeamRight[Fi][Ti].vx[Fr] = 0;
              TeamRight[Fi][Ti].vy[Fr] = 0;
              TeamRight[Fi][Ti].body_angle[Fr] = 0;
              TeamRight[Fi][Ti].head_angle[Fr] = 0;
          }
      }
  }   
  </code></pre>
  
  <p>In destructor pointers are deleted as following:</p>
  
  <pre><code>for (int i = 0; i &lt; file_num; i++)
      {       
          for (int j = 0; j &lt; 11; j++)
          {
              delete TeamLeft[i][j].time;
              delete TeamLeft[i][j].side;
              delete TeamLeft[i][j].unum;
              delete TeamLeft[i][j].type;
              delete TeamLeft[i][j].state;
              delete TeamLeft[i][j].x;
              delete TeamLeft[i][j].y;
              delete TeamLeft[i][j].vx;
              delete TeamLeft[i][j].vy;
              delete TeamLeft[i][j].body_angle;
              delete TeamLeft[i][j].head_angle;
  
              delete TeamRight[i][j].time;
              delete TeamRight[i][j].side;
              delete TeamRight[i][j].unum;
              delete TeamRight[i][j].type;
              delete TeamRight[i][j].state;
              delete TeamRight[i][j].x;
              delete TeamRight[i][j].y;
              delete TeamRight[i][j].vx;
              delete TeamRight[i][j].vy;
              delete TeamRight[i][j].body_angle;
              delete TeamRight[i][j].head_angle;
          }
      }
      for (int i = 0; i &lt; file_num; i++)
      {
          delete Ball[i].time;
          delete Ball[i].x;
          delete Ball[i].y;
          delete Ball[i].vx;
          delete Ball[i].vy;
  
          delete TeamLeft[i];
          delete TeamRight[i];
  
          delete TeamScore[i].time;       
          delete TeamScore[i].score_l;        
          delete TeamScore[i].score_r;
          delete TeamScore[i].pen_score_l;
          delete TeamScore[i].pen_score_r;
          delete TeamScore[i].pen_miss_l;
          delete TeamScore[i].pen_miss_r;
  
          delete playmode[i];
  
      }
  
      delete Ball;
      delete [] TeamScore;
      delete TeamLeft;
      delete TeamRight;
      delete playmode;
  </code></pre>
  
  <p>still there is a memory leakage and I tested it in task manager. if big arrays are created and destroyed the program used all available RAM and the program will be terminated with unhandled error. I will appreciate if anyone can help me with the problem of destructor.</p>
  
  <p>=========================================================================
  Updated version of destructor which will delete all allocated memories correctly is as:</p>
  
  <pre><code>    for (int i = 0; i &lt; file_num; i++)
      {
  
          for (int j = 0; j &lt; 11; j++)
          {
              delete[] TeamLeft[i][j].time;
              delete[] TeamLeft[i][j].side;
              delete[] TeamLeft[i][j].unum;
              delete[] TeamLeft[i][j].type;
              delete[] TeamLeft[i][j].state;
              delete[] TeamLeft[i][j].x;
              delete[] TeamLeft[i][j].y;
              delete[] TeamLeft[i][j].vx;
              delete[] TeamLeft[i][j].vy;
              delete[] TeamLeft[i][j].body_angle;
              delete[] TeamLeft[i][j].head_angle;
  
              delete[] TeamRight[i][j].time;
              delete[] TeamRight[i][j].side;
              delete[] TeamRight[i][j].unum;
              delete[] TeamRight[i][j].type;
              delete[] TeamRight[i][j].state;
              delete[] TeamRight[i][j].x;
              delete[] TeamRight[i][j].y;
              delete[] TeamRight[i][j].vx;
              delete[] TeamRight[i][j].vy;
              delete[] TeamRight[i][j].body_angle;
              delete[] TeamRight[i][j].head_angle;
          }
      }
      for (int i = 0; i &lt; file_num; i++)
      {  
  
          delete[] Ball[i].time;
          delete[] Ball[i].x;
          delete[] Ball[i].y;
          delete[] Ball[i].vx;
          delete[] Ball[i].vy;
  
          delete[] TeamLeft[i];
          delete[] TeamRight[i];
  
          delete[] TeamScore[i].time;
          delete[] TeamScore[i].score_l;
          delete[] TeamScore[i].score_r;
          delete[] TeamScore[i].pen_score_l;
          delete[] TeamScore[i].pen_score_r;
          delete[] TeamScore[i].pen_miss_l;
          delete[] TeamScore[i].pen_miss_r;
  
          delete[] playmode[i];
  
      }
  
      delete[] Ball;
      delete[] TeamScore;
      delete[] TeamLeft;
      delete[] TeamRight;
      delete[] playmode;
  }
  </code></pre>