I've been trying to integrate Qt and OpenCV and ran into a strange problem: some OpenCV functions cause access violations, but only when called during a timerEvent. I simplified the code as much as possible as a proof of concept (see below). The two functions that crash are cvCvtColor (when converting from BGR to Gray) and cvCanny when the sobel aperture has a size greater than 3. Both of these functions work when called anywhere else in the program (e.g. constructor or main). But when called from a timerEvent, an access violation crashes the program. Any ideas?

WinXP
Qt 4.6.1 (mingw)
OpenCV 2.0.0a

CvDebug.pro
Qt Code:
  1. TARGET = QtOpenCV
  2. TEMPLATE = app
  3.  
  4. INCLUDEPATH += C:\OpenCV\include\opencv
  5. LIBS += -L"C:\OpenCV\lib" -lcv200 -lhighgui200 -lcxcore200
  6. SOURCES += main.cpp CvDebug.cpp
  7. HEADERS += CvDebug.h
To copy to clipboard, switch view to plain text mode 

main.cpp
Qt Code:
  1. #include <QtGui/QApplication>
  2. #include "CvDebug.h"
  3.  
  4. int main(int argc, char **argv)
  5. {
  6. QApplication a(argc, argv);
  7. CvDebug w;
  8. w.show();
  9. return a.exec();
  10. }
To copy to clipboard, switch view to plain text mode 

CvDebug.h
Qt Code:
  1. #ifndef CVDEBUG_H
  2. #define CVDEBUG_H
  3.  
  4. #include <QtGui/QMainWindow>
  5. #include <cv.h>
  6. #include <highgui.h>
  7.  
  8. class CvDebug : public QMainWindow
  9. {
  10. Q_OBJECT
  11.  
  12. public:
  13. CvDebug(QWidget *parent = 0);
  14. ~CvDebug();
  15. CvCapture *camera;
  16.  
  17. protected:
  18. void timerEvent(QTimerEvent*);
  19. };
  20.  
  21. #endif // CVDEBUG_H
To copy to clipboard, switch view to plain text mode 

CvDebug.cpp
Qt Code:
  1. #include "CvDebug.h"
  2. #include <cv.h>
  3. #include <highgui.h>
  4. #include <cxcore.h>
  5.  
  6. CvDebug::CvDebug(QWidget *parent) : QMainWindow(parent)
  7. {
  8. cvNamedWindow( "Color", CV_WINDOW_AUTOSIZE );
  9. cvNamedWindow( "Gray", CV_WINDOW_AUTOSIZE );
  10. camera = cvCreateCameraCapture(0);
  11.  
  12. startTimer(100); // msec
  13. }
  14.  
  15. CvDebug::~CvDebug()
  16. {
  17. cvDestroyWindow( "Color" );
  18. cvDestroyWindow( "Gray" );
  19. cvReleaseCapture(&camera);
  20. }
  21.  
  22. void CvDebug::timerEvent(QTimerEvent*)
  23. {
  24. IplImage* image = cvQueryFrame( camera );
  25. cvShowImage( "Color", image);
  26. IplImage* grayImage = cvCreateImage( cvGetSize(image), IPL_DEPTH_8U, 1);
  27. cvCvtColor( image, grayImage, CV_BGR2GRAY );
  28. cvShowImage( "Gray", grayImage );
  29. }
To copy to clipboard, switch view to plain text mode