ofEvents question

Hey Guys,

I’m trying to understand how to use ofEvents and listeners/notifiers so I wrote up a really simple example. But apparently it’s not simple enough because it’s not working ;D

What I was trying to do was increment a counter through events. I’m not really sure if I’m doing any of this right but so far I’ve gotten it to compile into something. However, when I run it I get a “GDB program received signal EXC_BAD_ACCESS” error. In the past I’ve gotten this error when I was trying to improperly access an object. Here’s the code.

myListener.h

  
  
#ifndef ML  
#define ML  
  
#include "ofMain.h"  
#include "ofEvents.h"  
  
class myListener {  
	public:  
		myListener(){  
			counter = 0;  
		}  
	  
		int counter;  
		ofEvent<int> intEvent;  
		  
	void enable(){  
		ofAddListener(ofEvents.update, this, &myListener::update);  
	}  
	  
	void disable(){  
		ofRemoveListener(ofEvents.update, this, &myListener::update);  
	}  
	  
	void update(ofEventArgs & args){  
		counter++;  
		ofNotifyEvent(intEvent, counter, this);  
	}  
};  
  
#endif  
  

testapp.h

  
  
#ifndef _TEST_APP  
#define _TEST_APP  
  
  
#include "ofMain.h"  
#include "myListener.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 changeMSG(int & i);  
	  
		myListener* l;  
	  
		string msg;  
  
};  
  
#endif  
  

testApp.cpp

  
  
  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
	ofBackground(255, 255, 255);  
	ofAddListener(l->intEvent, this, &testApp::changeMSG); //causing EXC BAD ACCESS  
	ofSetColor(0, 0, 0);  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
	ofDrawBitmapString(msg, 100, 100);  
}  
  
//--------------------------------------------------------------  
void testApp::keyPressed(int key){  
  
}  
  
//--------------------------------------------------------------  
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::changeMSG(int & i){  
	msg = "The counter is at " + ofToString(i);  
}  
  

Does anything pop out as obviously wrong?

You’re not allocating myListener* l, somewhere you need to have

  
l = new myListener();   

or just have it be an instance of myListener, like:

  
myListener l;  

there’s also no reason for myListener l to be a pointer. in general if you can avoid dynamic memory allocation (pointers) it’s a good idea, and i can’t see that it’s necessary here. so if you drop the * you can change l->intEvent to l.intEvent and you’re set.