HEAP CORRUPTION DETECTED with openframeworks

hey guys, I trying to get my openframeworks to work, but when I exit, I get the HEAP CORRUPTION DETECTED error. also my output image is completely black for some odd reason, it worked before, tried to out-comment several block of code, but nothing helps. Do any of you know what the problem is ?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
 #include "testApp.h"
#include "math.h"
//--------------------------------------------------------------
void testApp::setup(){
	
	camWidth 		= 320;	// try to grab at this size. 
	camHeight 		= 280;
	coef = 1.0;
	tresholdMath	= 100;
	d				= 0;
	treshold		= 100;
	vidGrabber.setVerbose(true);
	vidGrabber.initGrabber(camWidth,camHeight);
	videoInverted 	= new unsigned char[camWidth*camHeight*3];
	videoTexture.allocate(camWidth,camHeight, GL_RGB);	
	delete videoInverted;
}


//--------------------------------------------------------------
void testApp::update(){
	
	ofBackground(100,100,100);
	
	vidGrabber.grabFrame();
	
	if (vidGrabber.isFrameNew()){
		int totalPixels = camWidth*camHeight*3;
		unsigned char * pixels = vidGrabber.getPixels();
		for (int i = 0; i < totalPixels; i++){
			videoInverted[i] = pixels[i];
			videoInverted[i] = pow((videoInverted[i]/255.0),coef)*255;
		}
		
		if (flags[0]) //invert
			for (int i = 0; i < totalPixels; i++){
				videoInverted[i] = 255 - videoInverted[i];
				 
	
			}
			
			if (flags [3]) //gray
				for (int y = 0; y < camHeight; y++){
					for (int x = 0; x < camWidth; x++){
					int gray = (videoInverted[(y*camWidth+x)*3+0]+videoInverted[(y*camWidth+x)*3+1]+videoInverted[(y*camWidth+x)*3+2])/3.0;
					videoInverted[(y*camWidth+x)*3+0] = gray;
					videoInverted[(y*camWidth+x)*3+1] = gray;
					videoInverted[(y*camWidth+x)*3+2] = gray;
					
					
					}
				
				}
			
				if (flags[4]) //treshold

				for (short y=0; y<camHeight; y++){
					for (short x=0; x<camWidth; x++) {
				if (videoInverted[(y*camWidth+x)*3+0] < 128){
					videoInverted[(y*camWidth+x)*3+0] = 0;
					videoInverted[(y*camWidth+x)*3+1] = 0;
					videoInverted[(y*camWidth+x)*3+2] = 0;
				} else {
					videoInverted[(y*camWidth+x)*3+0] = 255;
					videoInverted[(y*camWidth+x)*3+1] = 255;
					videoInverted[(y*camWidth+x)*3+2] = 255;
				}
			
			
		}
	}
			

			if (flags[9]) //segmentation color tracking
				for (int y=0; y <camHeight; y++){
					for (int x=0; x<camWidth; x++){
					
					
					tresholdMath = videoInverted[x+(y*camWidth*3)+1]+videoInverted[x+(y*camWidth*3)+2];
				if (videoInverted[(y*camWidth+x)*3+0]/(videoInverted[(y*camWidth+x)*3+0]+videoInverted[(y*camWidth+x)*3+1]+videoInverted[(y*camWidth+x)*3+2]+ 0.01) > 0.7 && videoInverted[videoInverted[(y*camWidth+x)*3+0] > 50]){
				
					videoInverted[(y*camWidth+x)*3+0] = 255;
					videoInverted[(y*camWidth+x)*3+1] = 255;
					videoInverted[(y*camWidth+x)*3+2] = 255;
				}
				else{

					videoInverted[(y*camWidth+x)*3+0] = 0;
					videoInverted[(y*camWidth+x)*3+1] = 0;
					videoInverted[(y*camWidth+x)*3+2] = 0;

				}
			}
				
		}
				
	
	 //RED CHANNEL - 'R'
	if (flags[11]){
	 for (int y=0; y < camHeight; y++){
	  for(int x=0; x < camWidth; x++){ 
		 videoInverted[(y*camWidth+x)*3+1] =0; 
			 videoInverted[(y*camWidth+x)*3+2] =0; 
    }
   }
  }

	 //Green CHANNEL - 'G'
	if (flags[12]){
	 for (int y=0; y < camHeight; y++){
		 for(int x=0; x < camWidth; x++){ 
     videoInverted[(y*camWidth+x)*3+0] =0; 
     videoInverted[(y*camWidth+x)*3+2] =0; 
    }
   }
  }

   //Blue CHANNEL - 'B'
  if (flags[13]){
   for (int y=0; y < camHeight; y++){
    for(int x=0; x < camWidth; x++){ 
     videoInverted[(y*camWidth+x)*3+0] =0; 
     videoInverted[(y*camWidth+x)*3+1] =0; 
    }
   }
  }

  if (flags[14]) //green detection
				for (int y=0; y <camHeight; y++){
					for (int x=0; x<camWidth; x++){
					
					
				
				if (videoInverted[(y*camWidth+x)*3+1]/(videoInverted[(y*camWidth+x)*3+1]+videoInverted[(y*camWidth+x)*3+0]+videoInverted[(y*camWidth+x)*3+2]+ 0.01)){
				
					(videoInverted[(y*camWidth+x)*3+1]/(videoInverted[(y*camWidth+x)*3+0]+videoInverted[(y*camWidth+x)*3+2]+videoInverted[(y*camWidth+x)*3+1]+ 0.01)>0.7 && videoInverted[(y*camWidth+x)*3+0] >50);
					videoInverted[(y*camWidth+x)*3+0] = 255;
					videoInverted[(y*camWidth+x)*3+1] = 255;
					videoInverted[(y*camWidth+x)*3+2] = 255;
				}
				else{

					videoInverted[(y*camWidth+x)*3+0] = 0;
					videoInverted[(y*camWidth+x)*3+1] = 0;
					videoInverted[(y*camWidth+x)*3+2] = 0;

				}
			}
				
		}
				if (flags[15]) //red detection
				for (int y=0; y <camHeight; y++){
					for (int x=0; x<camWidth; x++){
					
					
				
				if (videoInverted[(y*camWidth+x)*3+0]/(videoInverted[(y*camWidth+x)*3+0]+videoInverted[(y*camWidth+x)*3+1]+videoInverted[(y*camWidth+x)*3+2]+ 0.01)){
				
					(videoInverted[(y*camWidth+x)*3+0]/(videoInverted[(y*camWidth+x)*3+1]+videoInverted[(y*camWidth+x)*3+2]+videoInverted[(y*camWidth+x)*3+0]+ 0.01)>0.7 && videoInverted[(y*camWidth+x)*3+0] >50);
					videoInverted[(y*camWidth+x)*3+0] = 255;
					videoInverted[(y*camWidth+x)*3+1] = 255;
					videoInverted[(y*camWidth+x)*3+2] = 255;
				}
				else{

					videoInverted[(y*camWidth+x)*3+0] = 0;
					videoInverted[(y*camWidth+x)*3+1] = 0;
					videoInverted[(y*camWidth+x)*3+2] = 0;

				}
			}
				
		}

					if (flags[16]) //blue detection
				for (int y=0; y <camHeight; y++){
					for (int x=0; x<camWidth; x++){
					
					
				
				if (videoInverted[(y*camWidth+x)*3+2]/(videoInverted[(y*camWidth+x)*3+2]+videoInverted[(y*camWidth+x)*3+1]+videoInverted[(y*camWidth+x)*3+0]+ 0.01)){
				
					(videoInverted[(y*camWidth+x)*3+2]/(videoInverted[(y*camWidth+x)*3+1]+videoInverted[(y*camWidth+x)*3+0]+videoInverted[(y*camWidth+x)*3+2]+ 0.01)>0.7 && videoInverted[(y*camWidth+x)*3+0] >50);
					videoInverted[(y*camWidth+x)*3+0] = 255;
					videoInverted[(y*camWidth+x)*3+1] = 255;
					videoInverted[(y*camWidth+x)*3+2] = 255;
				}
				else{

					videoInverted[(y*camWidth+x)*3+0] = 0;
					videoInverted[(y*camWidth+x)*3+1] = 0;
					videoInverted[(y*camWidth+x)*3+2] = 0;

				}
			}
				
		}

		videoTexture.loadData(videoInverted, camWidth,camHeight, GL_RGB);
		//delete videoInverted;
	}

}
/*centerOfMassX = 0;
centerOfMassY = 0;
numOfPix=0;*/

//--------------------------------------------------------------
void testApp::draw(){
	ofSetHexColor(0xffffff);
	vidGrabber.draw(20,20);
	videoTexture.draw(20+camWidth,20,camWidth,camHeight);
}


//--------------------------------------------------------------
void testApp::keyPressed  (int key){ 
	
	
	
	switch (key){
	
	case 'i':
		flags[0] = !flags[0];
		break;

	case '+':
		coef/=1.2;
		break;

	case '-':
		coef*=1.2;
		break;


	case 'o':
		flags[3] = !flags[3];
		break;

	case 't':
		flags[4] = !flags[4];
		break;

	case 'm':
		flags[5] = !flags[5];
		break;
		
	case 'e':
		flags [6] =!flags[6];
		break;

	case 'd':
		flags [7] = !flags[7];
		break;

	case 'l':
		flags[9] =!flags[9];
		break;

	case 'q':
		flags[10] = !flags[10];
		break;

	case 'R':
		flags[11] = !flags[11];
		break;

	case 'g':
		flags[12] = !flags[12];
		break;

	case 'b':
		flags[13] = !flags[13];
		break;

	case 'w':
		flags[14] =!flags[14];
		break;

	case 's':
		flags[15] =!flags[15];
		break;

	case 'x':
		flags[16] =!flags[16];
		break;
	}

	}
	
	


//--------------------------------------------------------------
void testApp::keyReleased(int key){ 
	
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
	
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
	
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
	
}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){ 

}
[code]
Build with debug symbols and run it under valgrind or some similar memory watcher, and it will tell you when you are trashing your memory. Something is writing over memory it should not be.
Topic archived. No new replies allowed.