title: Why my program is consuming a bunch of memory?
body: <p>I'm doing a game based in layers(to make a parallax effect) and I need to crop a image from each layer to compose a scene. But something is acumulating in memory and I don't know what is. I filled my code with <code>delete[]</code> as a desperate measure to fix.</p>
  
  <p><strong>ImageClass.h</strong></p>
  
  <pre><code>#include &lt;math.h&gt;
  
  class Image {
  public: 
      Image(int w, int h){
       width = w; height = h;
       pixels = new int[w*h*3];
    }
  
      Image(){}
  
    void setPixel(int a, int r, int g, int b, int x, int y){
        pixels[x + y * width] = (a &lt;&lt; 24) | (r &lt;&lt; 16) | (g &lt;&lt; 8) | b;
    }
  
    int getPixel(int x, int y){
       return pixels[x+y*width];
    }
    int getWidth(){ return width; }
  
    int getHeight(){ return height; }
  
    int* getPixels() {
        return pixels;
    }
  
    void composition(int xi, int yi, int *foreground, int fw, int fh) {
        for (int i = yi; (i &lt; fh) &amp;&amp; (i &lt; height); i++) {
            for (int j = xi; (j &lt; fw) &amp;&amp; (j &lt; width); j++) {
                int aF = (foreground[j + i * fw] &gt;&gt; 24) &amp; 0xff;
                int rF = (foreground[j + i * fw] &gt;&gt; 16) &amp; 0xff;
                int gF = (foreground[j + i * fw] &gt;&gt; 8) &amp; 0xff;
                int bF = (foreground[j + i * fw]) &amp; 0xff;
  
                int rI = (pixels[j + i * width] &gt;&gt; 16) &amp; 0xff;
                int gI = (pixels[j + i * width] &gt;&gt; 8) &amp; 0xff;
                int bI = (pixels[j + i * width]) &amp; 0xff;
  
                float am = aF / (float)255;
  
                int c1 = (rF * am) + rI * (1 - am);
                int c2 = (gF * am) + gI * (1 - am);
                int c3 = (bF * am) + bI * (1 - am);
  
                pixels[j + i * width] = (aF &lt;&lt; 24) | (c1 &lt;&lt; 16) | (c2 &lt;&lt; 8) | c3;
            }
        }
        delete[] foreground;
    }
  
  private:
      int *pixels;
          int width, height;
  };
  </code></pre>
  
  <hr>
  
  <p><strong>Layer.h</strong></p>
  
  <pre><code>#include &lt;fstream&gt;
  #include "ImageClass.h"
  
  using namespace std;
  
  class Layer {
      private:
          Image *img;
          float scrollY;
          float scrollX;
          float ypos;
          float xpos;
  
      public:
          Layer(){
              img = NULL;
              scrollX = 0;
              scrollY = 0;
              ypos = 0;
              xpos = 0;
          }
  
          Layer(Image *pImg, float sy, float sx, float yp, float xp) {
              img = pImg;
              scrollX = sx;
              scrollY = sy;
              ypos = yp;
              xpos = xp;
          }
  
          Layer(char* path, float sy, float sx, float yp, float xp) {
              ifstream arg(path);
              if (!arg.is_open()) {
                  exit(EXIT_FAILURE);
              }
  
              char word[10];
  
              //Reading type
              arg &gt;&gt; word;
  
              //Reading width
              int argWidth;
              arg &gt;&gt; word;
              argWidth = atoi(word);
  
              //Reading height
              int argHeight;
              arg &gt;&gt; word;
              argHeight = atoi(word);
  
              //Reading max
              arg &gt;&gt; word;
  
              //Creating an image with arg size
              img = new Image(argWidth, argHeight);
  
              int a, r, g, b;
              for (int i = 0; i &lt; argHeight; i++) {
                  for (int j = 0; j &lt; argWidth; j++) {
                      arg &gt;&gt; word;
                      a = atoi(word);
                      arg &gt;&gt; word;
                      r = atoi(word);
                      arg &gt;&gt; word;
                      g = atoi(word);
                      arg &gt;&gt; word;
                      b = atoi(word);
                      img-&gt;setPixel(a, r, g, b, j, i);
                  }
              }
  
              arg.close();
              //End of reading
  
              scrollX = sx;
              scrollY = sy;
              ypos = yp;
              xpos = xp;
          }
  
          int* subImage(int wp, int hp) {
              int* subPixels = new int[wp * hp];
              for (int i = 0; (i &lt; hp) &amp;&amp; (i &lt; img-&gt;getHeight()); i++) {
                  for (int j = 0; (j &lt; wp) &amp;&amp; (j &lt; img-&gt;getWidth()); j++) {
                      subPixels[j + i * wp] = img-&gt;getPixel(j + xpos, i + ypos);
                   }
              }
              return subPixels;
              delete[] subPixels;
          }
  
          void horizontalScrolling(bool direction){
              //if direction equals true, it goes to the right. Else, left
              if (direction) {
                  xpos += scrollX;
              } else {
                  xpos -= scrollX;
              }
          }
  
          void verticalScrolling(bool direction){
              //if direction equals true, it goes up. Else, down
              if (direction) {
                  ypos -= scrollY;
              } else {
                  ypos += scrollY;
              }
          }
  
          float getScrollX() {
              return scrollX;
          }
  
          float getScrollY() {
              return scrollY;
          }
  
          float getPosX() {
              return xpos;
          }
  
          float getPosY() {
              return ypos;
          }
  
          void setPosX(float xp){
              xpos = xp;
          }
  
          void setPosY(float yp){
              ypos = yp;
          }
  
          Image* getImage() {
              return img;
          }
  
  };
  </code></pre>
  
  <hr>
  
  <pre><code>#include "Layer.h"
  #include &lt;math.h&gt;
  
  class Scene {
      private:
          int width;
          int height;
          Layer *layers[10];
          Image *finalScene;
  
      public:
          Scene(int w, int h) {
              width = w;
              height = h;
              finalScene = new Image(w, h);
              loadLayers();
          }
  
          void loadLayers() {
              //loading layer 0
              layers[0] = new Layer("Background", 1, 1.5, 0, 0);
              layers[1] = new Layer("Island", 5, 5, 0, 0);
          }
  
          void mountScene() {
              delete[] finalScene;
              finalScene = new Image(width, height);
              finalScene-&gt;composition(0, 0, layers[0]-&gt;subImage(width, height), width, height);
              finalScene-&gt;composition(0, 0, layers[1]-&gt;subImage(width, height), width, height);
          }
  
          void sceneHScrolling(bool direction) {
              int cont = 0;
              int layersCreated = 2;
              while (cont &lt; layersCreated) {
                  int delta = layers[cont]-&gt;getImage()-&gt;getWidth() - width;
                  int posX = layers[cont]-&gt;getPosX();
                  if (direction) {
                      if (posX &lt; (delta)) {
                          layers[cont]-&gt;horizontalScrolling(direction);
                      }
                  } else {
                      if (posX
   &gt; 0) {
                          layers[cont]-&gt;horizontalScrolling(direction);
                      }
                  }
                  cont++;
              }
          }
  
          void sceneVScrolling(bool direction) {
              int cont = 0;
              int layersCreated = 2;
              while (cont &lt; layersCreated) {
                  int delta = layers[cont]-&gt;getImage()-&gt;getHeight() - height;
                  int posY = layers[cont]-&gt;getPosY();
                  if (direction) {
                      if (posY &gt; 0) {
                          layers[cont]-&gt;verticalScrolling(direction);
                      }
                  } else {
                      if (posY &lt; delta) {
                          layers[cont]-&gt;verticalScrolling(direction);
                      }
                  }
                  cont++;
              }
          }
  
          int* getScene() {
              return finalScene-&gt;getPixels();
          }
  
          Layer* getLayer(int i){
              return layers[i];
          }
  };
  </code></pre>