variable definition order in header file

hello:

I’m working on an application which saves frames that come from a webcam and also controls a stepper motor using ofSerial (through an arduino board). The application is working fine, but now I’m trying to make the code a little bit more tidy because it will soon be longer and I want to keep it as organized as possible. The code has pieces of movieGrabberExample, serialExample and imageSaverExample.

On the header file (testApp.h) I define all the variables I use and while I found out that changing the place of the definition of a variable on the header file makes the application fail to start. The easy way would be leaving the definition in the place where the application works but I want to understand why this happens because I wasn’t expecting this behavior. I thought that the order in which variables are defined in the header file didn’t affect the application.

The way it works is the following:

  
#include "ofMain.h"  
  
class testApp : public ofBaseApp{  
	  
	public:  
		  
		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 sacarFoto();  
		void avanzarUnPaso();  
		void exit();  
		  
		ofVideoGrabber 		vidGrabber;  
		unsigned char * 	videoInverted;  
		ofTexture			videoTexture;  
		int 				camWidth;  
		int 				camHeight;  
		int					origenX;  
		int					origenY;  
  
		//VARIABLES PARA CAPTURA DE IMAGENES  
  
		int					cantidadCapturas;  
		char 				snapString[255];  
		ofImage 			img;  
		//ofTrueTypeFont		cooper;  
  
		ofTrueTypeFont		font;;  
		bool 				bSnapshot;  
		float 				phase;  
  
		//VARIABLES PARA COMUNICACION SERIAL  
  
		bool		bSendSerialMessage;			// a flag for sending serial  
		int			lectura;   
		//char		bytesRead[3];				// data from serial, we will be trying to read 3  
		//char		bytesReadString[4];			// a string needs a null terminator, so we need 3 + 1 bytes  
		//int			nBytesRead;					// how much did we read?  
		//int			nTimesRead;					// how many times did we read?  
		//float		readTime;					// when did we last read?				  
		  
		ofSerial	serial;  
		int					contadorFotos;  
		int					cantidadFotos;				  
		int					contadorCapturas;  
  
};  
  
#endif	  
  

And the way it doesn’t work is:

  
#ifndef _TEST_APP  
#define _TEST_APP  
  
  
#include "ofMain.h"  
  
class testApp : public ofBaseApp{  
	  
	public:  
		  
		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 sacarFoto();  
		void avanzarUnPaso();  
		void exit();  
		  
		ofVideoGrabber 		vidGrabber;  
		unsigned char * 	videoInverted;  
		ofTexture			videoTexture;  
		int 				camWidth;  
		int 				camHeight;  
		int					origenX;  
		int					origenY;  
  
		//VARIABLES PARA CAPTURA DE IMAGENES  
  
		int					cantidadCapturas;  
		char 				snapString[255];  
		ofImage 			img;  
		//ofTrueTypeFont		cooper;  
  
		ofTrueTypeFont		font;;  
		bool 				bSnapshot;  
		float 				phase;  
		int					contadorFotos;  
		int					cantidadFotos;				  
		int					contadorCapturas;  
  
		//VARIABLES PARA COMUNICACION SERIAL  
  
		bool		bSendSerialMessage;			// a flag for sending serial  
		int			lectura;   
		//char		bytesRead[3];				// data from serial, we will be trying to read 3  
		//char		bytesReadString[4];			// a string needs a null terminator, so we need 3 + 1 bytes  
		//int			nBytesRead;					// how much did we read?  
		//int			nTimesRead;					// how many times did we read?  
		//float		readTime;					// when did we last read?				  
		  
		ofSerial	serial;  
  
  
};  
  
#endif	  
  

The differences are that the definition of 3 counters (int contadorFotos, int cantidadFotos, int contadorCapturas) was moved up.

The error message I get is: Unhandled exception at 0x004e2cfb in moduloCapturaPaPcapturas_debug.exe: 0xC0000005: Access violation reading location 0xcdcdcd00.

Can anyone tell me why is this happening?
thanks!

the problem has nothing to do with the order of the variables. inside your code you’re trying to access some memory that isn’t initialized. the reason why depending on the order of the variables the program crashes is that depending on how the variables of your application are layered in memory sometimes the bad access goes inside a region that is part of your application, but when you change the order that memory address is not anymore inside what the os assigns to your app and it crashes.

in both cases there’s something wrong with how your creating/accesing memory, probably an array of the wrong size or a loop trying to access more elements of an array that it has.

Arturo, thanks for your reply!
I’ll check the initialization of my variables.