Heap corruption when trying to create an image from pixel array


#1

Hello, I’m trying to create an image from a pixel array, as seen in the book “Mastering openFrameworks” by Denis Perevalov in page 116.

The debug error says the following:
Debug Error!

Program:
(path)

HEAP CORRUPTION DETECTED: after Normal block(#1573) at 0x0929F040.
CTR detected that the application wrote to memory after end of heap buffer.

I already searched for and answer but I didn’t found nothing that helped me.

Thanks in advance!

(Also, this is my first post, any recommendations to make better posts?)


#2

Hi Welcome! :slight_smile:
Feel free to ask whatever in case of doubt in this forum. Thats the whole point of it.
If you paste the code that you are trying to use it will be very helpful. otherwise it is quite hard to know what might be the problem.
Also pointing out what OS you are using, which IDE and openFrameworks version is very useful.

Also, when pasting code into the forum’s text field, once pasted select it all and press the </> button in the toolbar. It will format the code nicely so it can be more readable.
cheers


#3

Thanks!

I’m using Windows 10, Visual Studio 2017 and oF v0.10.1

This is the code:

#include "ofApp.h"



//--------------------------------------------------------------
void ofApp::setup(){

}

//--------------------------------------------------------------
void ofApp::update(){
	//Creating image

	int w = 512;
	int h = 512;

	//Allocate array for filling pixels data
	unsigned char *data = new unsigned char[w * h * 4];

	//Fill array for each pixel (x, y)
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			//Compute preliminary values,
			//needed for our pixel color calculations:

			//1. Time from application start

			float time = ofGetElapsedTimef();

			//2. Level of hyperbola value of x and y width
			//center in w / 2, h / 2
			float v = (x - x / 2) * (y - h / 2);

			//3. Combining v with time for motion effect
			float u = v * 0.00025 * time;
			//Here 0.0025 was chosen empirically

			//4. Compute color components as periodical 
			//functions of u, and stretched to [0...255]
			int red = ofMap(sin(u), -1, 1, 0, 255);
			int green = ofMap(sin(u * 2), -1, 1, 0, 255);
			int blue = 255 - green;
			int alpha = 255;	//Just constant for simplicity
			
			//Fill array components for pixel (x, y):
			int index = 4 * (x + w * h);
			data[index] = red;
			data[index + 1] = green;
			data[index + 2] = blue;
			data[index + 3] = alpha;
		}
	}

	//Load array to image
	image.setFromPixels(data, w, h, OF_IMAGE_COLOR_ALPHA);

	//Array is not needed anymore, so clear memory
	delete[] data;
}

//--------------------------------------------------------------
void ofApp::draw(){
	ofBackground(255, 255, 255);		//Set up white background
	ofSetColor(255, 255, 255);			//Set color for image drawing
	image.draw(0, 0);					//Draw image
}


Also, I noticed that when I erase line 58 (delete[] data;) the debug error disappears, but nothing is shown in the window.


#4

Hi,
I personally don’t like that way of creating pixels, first because it uses a raw pointer and the subsequent delete, as well as that it is constantly allocating new memory instead of just reusing.
instead I would allocate image in the setup and then just update its pixels.
There were a few wrong calculations too that I corrected.
it should look something like :

#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
	int w = 512;
	int h = 512;
	
	//Allocate array for filling pixels data
	image.allocate(w, h, OF_IMAGE_COLOR_ALPHA);
	
}

//--------------------------------------------------------------
void ofApp::update(){
	
	int w = image.getWidth();
	int h = image.getHeight();
	
	float time = ofGetElapsedTimef();
	
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			//Compute preliminary values,
			//needed for our pixel color calculations:
			
			//2. Level of hyperbola value of x and y width
			//center in w / 2, h / 2
			float v = (x - w / 2) * (y - h / 2);
			
			//3. Combining v with time for motion effect
			float u = v * 0.00025 * time;
			//Here 0.0025 was chosen empirically
			
			//4. Compute color components as periodical 
			//functions of u, and stretched to [0...255]
			int red = ofMap(sin(u), -1, 1, 0, 255);
			int green = ofMap(sin(u * 2), -1, 1, 0, 255);
			int blue = 255 - green;
			int alpha = 255;	//Just constant for simplicity
			
			//Fill array components for pixel (x, y):
			unsigned char * data = image.getPixels().getData(); 
			int index = 4 * (x + w * y);
			data[index] = red;
			data[index + 1] = green;
			data[index + 2] = blue;
			data[index + 3] = alpha;
		}
	}
	image.update();
}

//--------------------------------------------------------------
void ofApp::draw(){
	ofBackground(255, 255, 255);		//Set up white background
	ofSetColor(255, 255, 255);			//Set color for image drawing
	image.draw(0, 0);					//Draw image
	ofDrawBitmapStringHighlight("FPS: " + ofToString(ofGetFrameRate()), 20, 600);
}

cheers


#5

Thanks! It works just fine, I’m implementing your method in other exercises