Cannot exit with Esc

hi guys~ i just write a simple project with OpenCV functions directly called. but i find i couldn’t exit the application successfully with hitting Esc. Could anybody figure out why?
p.s. i remember that once upon a time, CCV had this problem too. so is it because the direct use of OpenCv functions?
Here is the testApp.h

  
  
#ifndef _TEST_APP  
#define _TEST_APP  
  
  
#include "ofMain.h"  
#include "ofxOpenCv.h"  
#include "ofxDirList.h"  
#include "ofxVectorMath.h"  
#include "ofxNetwork.h"  
#include "ofxOsc.h"  
#include "ofxThread.h"  
#include "ofxXmlSettings.h"  
#include "ofx3DModelLoader.h"  
#include "highgui.h"  
class testApp : public ofBaseApp{  
  
	public:  
  
		testApp();  
		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 resized(int w, int h);  
  
private:  
	CvCapture *cap;  
	IplImage * image, *grayImage;  
	int wid,hei;  
	int deviceID;  
	ofxCvGrayscaleImage ofcvGrayImage;  
  
};  
  
#endif  
  
  

And here is the testApp.cpp

  
  
#include "testApp.h"  
#include "stdio.h"  
  
//--------------------------------------------------------------  
testApp::testApp(){  
	  
}  
  
//--------------------------------------------------------------  
void testApp::setup(){  
	deviceID=0;  
	cap=cvCreateCameraCapture(deviceID);  
	image=cvQueryFrame(cap);  
	wid=image->width;  
	hei=image->height;  
	grayImage=cvCreateImage(cvSize(wid,hei),IPL_DEPTH_8U,1);  
	ofcvGrayImage.allocate(wid,hei);  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
	image=cvQueryFrame(cap);  
	cvCvtColor(image,grayImage,CV_BGR2GRAY);  
	ofcvGrayImage.setFromPixels((unsigned char*)grayImage->imageData,wid,hei);  
}  
  
  
//--------------------------------------------------------------  
void testApp::draw(){  
	ofcvGrayImage.draw(0,0);  
}  
  
//--------------------------------------------------------------  
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::resized(int w, int h){  
  
}  
  
  
  

Hi @yaya,

I’m gonna guess: maybe you need to release all your cv variables when your app exits.
Basically, when you do :

You ask openCv to instantiate a new object of type CvCapture, which complexity is unknown to us, and make your variable cap point to it.
Maybe, this CvCapture object is running threads or weird things that, while running, prevent your app to exit to avoid crashing or trigger undefined behaviour.

So, before exiting your app, you need to tell your cap object to clean up before exiting. The same way you instantiate a new object, you can ask to release it:

void cvReleaseCapture( CvCapture** capture );

Note that the parameter is a CvCapture**, a pointer to a pointer. Which means that you have to pass the address of your cap variable: &cap.

Now, luckily for us, the ofBaseApp class, which your testApp inherits from, declares an exit() function that will be called when your app wants to exit, and that you can override:

void testApp::exit()
{
   cvReleaseCapture(&cap);
}

Hope this helps !