crash with static var (i poco i think)

I have the following code in a header file.

What I would like is for the variable ‘mouse’ to exist as a global variable with the relevant methods and properties. However I get a crash and immediate exit at app launch. If I extern the var and init in a cpp I get the same problem. If I declare the variable as a property of testApp or any other class it works. I’m guessing it has something to do with ofAppEvents or ofMouseEvents?

This was working with 00573 (the zip theo posted in the ofxSimpleGUI post), but I updated everything from SVN and since then it doesnt work. Any ideas on if whether this is a bug or a ‘feature’? And why it was working but isn’t any more?

error message in console:

terminate called after throwing an instance of ‘Poco::SystemException’
what(): System exception

class ofxMouse : public ofMouseListener, public ofAppListener {  
	int x, y;			// current x, y  
	int px, py;			// previous x, y  
	int vx, vy;			// velocity x, y  
	bool button[MAX_MOUSE_BUTTONS];	// is the nth button down  
	ofxMouse() {  
		for(int i=0; i<MAX_MOUSE_BUTTONS; i++) button[i] = false;  
	string toString() {  
		char sz[255];  
		sprintf(sz, "MOUSE Position: (%i, %i) | Previous:  (%i, %i) | Velocity:  (%i, %i) | Button1: %i | Button2: %i | Button3: %i \n", x, y, px, py, vx, vy, button[0], button[1], button[2] );  
		return string(sz);  
	void update() {  
		vx = x - px;  
		vy = y - py;  
	void draw() {  
		px = x;  
		py = y;  
	void mouseMoved( int _x, int _y ) {  
		x = _x;  
		y = _y;  
	void mouseDragged( int _x, int _y, int _button ) {  
		x = _x;  
		y = _y;  
	void mousePressed( int _x, int _y, int _button ) {  
		x = _x;  
		y = _y;		  
		button[_button] = true;  
	void mouseReleased() {  
	void mouseReleased(int _x, int _y, int _button ) {  
		x = _x;  
		y = _y;  
		button[_button] = false;  
static ofxMouse mouse;  

Just tried, and this is working for me. i’m using too the last svn version. Can you post a little example where it crash?

Hey arturo, its crashing for me even with a completely empty default testApp, i.e. my testApp.h is:

class testApp : public ofSimpleApp{  
		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();  

and all those functions are empty in testApp.cpp
and I have the ofxMouse.h included.

It crashes on startup, strange that it works on yours!

I’m guessing its an initialization order thing? in ofxMouse instead of

static ofxMouse mouse  

if I declare

static ofxMouse *mouse   

and in testApp::setup()

mouse = new ofxMouse  

then it works fine. But my whole aim was to make it easy as possible, a global mouse object which has all the mouse properties I want in easy access without me havin to initialize it in the app. If there is no way around this crash bug I’ll just make it a singleton… but its weird that it works on yours! maybe a compiler thing? (linux vs mac?)

I’ve been looking some documentation for static/extern declarations and I think it has to do with the order of the cpp files in the generated makefile/call to gcc. Surely when you substituted your openframeworks version with that in the svn, the cpps in openframeworks became the last ones.

Try deleting ofxMouse and adding it again so the order of the files change.

This will tell you if that’s the problem, but of course is not a good solution. I think the only way to be sure is by using a singleton. To make the syntax simpler you can have a function or define that returns the ofxMouse instance, something like:

ofxMouse mouse(){  
  return ofxMouse::getInstance();  


#define OFX_MOUSE ofxMouse::getInstance()  

indeed perhaps we should have something like this for the events to avoid extern or static declarations, I’ve been using them lately, and are really problematic

ok, well my ofxMouse was just a header file, with static declaration, so I made it an extern, created a cpp file for it, and added that - and it does indeed work. But before it was like that and still crashing. So basically I think you’re right, it is the order in which they get added to the project - and so way too dodgy to use in this manner! I think I will stick to singleton… (its actually better I guess, forcing a global variable called ‘mouse’ may be dangerous)

you’re on OSX right? i’ve noticed some pretty bloody weird behaviour with static variables in the past. not sure whether it’s related to oF or just Apple’s GCC toolchain (i haven’t done very much programming on OSX that wasn’t oF based).

yea osx. what kind of weird behaviour are you talking about?

similar stuff, mainly crashes trying to initialize member objects, eg STL vectors. when debugging, eg stepping into the constructor of an instance declared static, sometimes they seem to have bogus memory addresses, when i’m 99% sure that the operating system or at least the C++ bootloader/whateveritscalled is supposed to automatically allocate heap memory for them.

anyway. solution for me was to just give up on having statically declared instances, and use the Singleton design pattern instead.

oh yea, actually i also noticed that sometimes the value of the static variable would not be correct in all scopes, even though I was sure that there wasn’t a variable with the same name redefined in that scope (i.e. I give the static variable a value in testApp, and in MyOtherClass it has a different value!!). Using extern variables solved that issue…

actually i also noticed that sometimes the value of the static variable would not be correct in all scopes

I’ve noticed this too on linux, I think it creates a different instance for each file that includes the .h where the variable is defined.

yup thats true.

if a static variable is defined in a global space (say at beginning of file) then this variable will be accessible only in this file (file scope).

So if a static variable is defined in a header file, then different cpp’s will have different instances… bad!

So if a static variable is defined in a header file, then different cpp’s will have different instances… bad!

well… this kind of makes sense actually. headers files aren’t compiled at all, they’re just included in their entirety within the cpp file at compile-time. the line #include “thing.h” is replaced with the entire contents of thing.h, in other words. so if you’re including thing.h in multiple different places, and thing.h declares a static variable, you’ll get multiple declarations of that variable.

and because one of the meanings of ‘static’ is ‘is inaccessable outside of the current file/function’, it makes sense you’ll get different instances each time this happens. intuitively it perhaps ought to throw a duplicate-symbols error at link time, but consider that you can have a static int counter at global scope in blob.cpp and another static int counter also at global scape in otherthing.cpp, and it will compile and run with no problems, each ‘counter’ being separate from the other.

an interesting thing to try is the ‘preprocess’ command on the context menu for cpp files in XCode. it shows you the raw code that is passed to the compiler, with all the comments stripped out, all the #includes included, and all the #defines expanded.