why is this eating up so much ram I don't understand

So I’m new with OF and C++ in general. I seem to have some serious ram management issues with something that seems fairly simple to me. I’m loading frames from a video into a texture, and frames from a camera feed into another texture and meshing them pixel for pixel of each frame into a third texture that i’m drawing to the screen. It all works fine for the first few seconds but then starts to eat an incredible amount of ram and I’m not sure what i’m doing wrong here:


#pragma once  
#include "ofMain.h"  
class testApp : public ofBaseApp{  
		void setup();  
		void update();  
		void draw();  
		void keyPressed  (int key);  
		void keyReleased(int key);  
		void mouseMoved(int x, int y );  
		void mouseDragged(int x, int y, int button);  
		void mousePressed(int x, int y, int button);  
		void mouseReleased(int x, int y, int button);  
		void windowResized(int w, int h);  
		void dragEvent(ofDragInfo dragInfo);  
		void gotMessage(ofMessage msg);  
    int w;  
    int h;  
    int totalPixels;  
    ofVideoGrabber videoCam;  
    ofVideoPlayer vidPlayer;  
    unsigned char * pixels;  
    ofTexture otherTex;  


#include "testApp.h"  
void testApp::setup(){  
    w = 1280;  
    h = 800;  
    totalPixels = w *h * 3;  
   // vidTex.allocate(w, h, GL_RGB);  
    otherTex.allocate(w, h, GL_RGB);  
  //  vidCamTex.allocate(w, h, GL_RGB);  
void testApp::update(){  
    pixels = new unsigned char[totalPixels];  
    //cout<< &pixels;  
    int counter = 0;  
    for (int i = 0; i<totalPixels; i+=3) {  
        if(counter % 2 == 0){  
            pixels[i] = videoCam.getPixels()[i];  
            pixels[i+1] = videoCam.getPixels()[i+1];  
            pixels[i+2] = videoCam.getPixels()[i+2];  
            pixels[i] = vidPlayer.getPixels()[i];  
            pixels[i+1] = vidPlayer.getPixels()[i+1];  
            pixels[i+2] = vidPlayer.getPixels()[i+2];  
    otherTex.loadData(pixels, w,h, GL_RGB);  
void testApp::draw(){  
    otherTex.draw(0, 0);  

You are allocating a new pixel array every time the update is called.

( pixels = new unsigned char[totalPixels]; )

Easiest solution is probably to move this line into your setup section.

pixels = new unsigned char[totalPixels];

somewhere after

totalPixels = w *h * 3;

Whenever you allocate an object/array/etc with the “new” keyword, is your responsibility to make sure that memory is freed somewhere with a call like:

delete[] pixels;

Since this is in your main app file, which is only used once, this is less critical, but just keep it in mind. It becomes very important when you are creating and destroying objects that allocate their own memory.

Thanks a bunch good sir!!