c++ - Why my program is consuming a bunch of memory? -


i'm doing game based in layers(to make parallax effect) , need crop image each layer compose scene. acumulating in memory , don't know is. filled code delete[] desperate measure fix.

imageclass.h

#include <math.h>  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 << 24) | (r << 16) | (g << 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) {       (int = yi; (i < fh) && (i < height); i++) {           (int j = xi; (j < fw) && (j < width); j++) {               int af = (foreground[j + * fw] >> 24) & 0xff;               int rf = (foreground[j + * fw] >> 16) & 0xff;               int gf = (foreground[j + * fw] >> 8) & 0xff;               int bf = (foreground[j + * fw]) & 0xff;                int ri = (pixels[j + * width] >> 16) & 0xff;               int gi = (pixels[j + * width] >> 8) & 0xff;               int bi = (pixels[j + * width]) & 0xff;                float = 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 + * width] = (af << 24) | (c1 << 16) | (c2 << 8) | c3;           }       }       delete[] foreground;   }  private:     int *pixels;         int width, height; }; 

layer.h

#include <fstream> #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 >> word;              //reading width             int argwidth;             arg >> word;             argwidth = atoi(word);              //reading height             int argheight;             arg >> word;             argheight = atoi(word);              //reading max             arg >> word;              //creating image arg size             img = new image(argwidth, argheight);              int a, r, g, b;             (int = 0; < argheight; i++) {                 (int j = 0; j < argwidth; j++) {                     arg >> word;                     = atoi(word);                     arg >> word;                     r = atoi(word);                     arg >> word;                     g = atoi(word);                     arg >> word;                     b = atoi(word);                     img->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];             (int = 0; (i < hp) && (i < img->getheight()); i++) {                 (int j = 0; (j < wp) && (j < img->getwidth()); j++) {                     subpixels[j + * wp] = img->getpixel(j + xpos, + ypos);                  }             }             return subpixels;             delete[] subpixels;         }          void horizontalscrolling(bool direction){             //if direction equals true, goes right. else, left             if (direction) {                 xpos += scrollx;             } else {                 xpos -= scrollx;             }         }          void verticalscrolling(bool direction){             //if direction equals true, 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;         }  }; 

#include "layer.h" #include <math.h>  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->composition(0, 0, layers[0]->subimage(width, height), width, height);             finalscene->composition(0, 0, layers[1]->subimage(width, height), width, height);         }          void scenehscrolling(bool direction) {             int cont = 0;             int layerscreated = 2;             while (cont < layerscreated) {                 int delta = layers[cont]->getimage()->getwidth() - width;                 int posx = layers[cont]->getposx();                 if (direction) {                     if (posx < (delta)) {                         layers[cont]->horizontalscrolling(direction);                     }                 } else {                     if (posx  > 0) {                         layers[cont]->horizontalscrolling(direction);                     }                 }                 cont++;             }         }          void scenevscrolling(bool direction) {             int cont = 0;             int layerscreated = 2;             while (cont < layerscreated) {                 int delta = layers[cont]->getimage()->getheight() - height;                 int posy = layers[cont]->getposy();                 if (direction) {                     if (posy > 0) {                         layers[cont]->verticalscrolling(direction);                     }                 } else {                     if (posy < delta) {                         layers[cont]->verticalscrolling(direction);                     }                 }                 cont++;             }         }          int* getscene() {             return finalscene->getpixels();         }          layer* getlayer(int i){             return layers[i];         } }; 

i filled code delete[] desperate measure fix.

you need bit more disciplined that. every new[] needs balanced delete[] , every new delete. adding delete calls more desperate measure: it's essential.

otherwise you'll leak memory, what's happening here.

alternatively, why not use std::vector<int> etc. instead? way, memory managed you.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -