Threading Help

Hi. This thread thing is confusing me, I just can’t get over it. Here’s my basic goal:

I want my app to be able to load images when I select them with an ofSystemLoadDiaglog(), then use Haar detection to find the faces in the images. I am just trying to learn threading, this is just a simple test I came up with. I have everything working fine, only when I select the image to load, it is supposed to then add it to my new threaded function where it uses opencv to detect the face. When I run the app it works fine until I try to load a new image. I always get EXC_BAD_ACCESS on this line in ofTexture: glGenTextures(1, (GLuint *)&texData.textureID);

Here is my threaded function .cpp file:

  
#include "haarThread.h"  
#include "ofMain.h"  
#include <iostream>  
  
haarThread::haarThread()  
:ofThread()  
{  
    imgPath = "";  
    haarImg.setUseTexture(false);  
      
}  
  
void haarThread::setup(string haarFilePath) {  
    lock();  
      
    haarFind.setup(haarFilePath);  
      
    unlock();  
      
}  
  
void haarThread::loadImagePath(string filename) {  
    lock();  
      
    imgPath = filename;  
      
    unlock();  
}  
  
void haarThread::threadedFunction() {  
      
    while(true) {  
        //cout << "my own custom thread!" << endl;  
        //ofSleepMillis(3000);  
        haarImg.setUseTexture(false);  
        haarImg.loadImage(imgPath);  
        haarFind.findHaarObjects(haarImg);  
        faceRect = haarFind.blobs[0].boundingRect;  
        haarImg.crop(faceRect.x, faceRect.y, faceRect.width, faceRect.height);  
  
    }  
  
}  

this is because you’re calling crop() in the threadedFunction, which tries to update() the texture. opengl data can only be referenced from the main thread. if you try to modify opengl data in other threads you will get a crash.

an alternative is to use ofPixels instead of ofImage. you can load into ofPixels using ofLoadImage(). ofPixels also supports crop().

Thanks Kyle. I removed the last two lines from my threaded class and I still get the error. I was looking through an example that jvcleave posted here: http://forum.openframeworks.cc/t/threading-and-opencv/3989/0 It looks like he had to modify the ofxCvHaarFinder class because it was using ofImage and he had to set it to setUseTexture(false). Does that make sense? Could that be why my thread is crashing, because I’m calling ofxCvHaarFinder and that uses opengl data? Would you kindly expand on the proper combination of opengl and threading?

Here’s my code now, I still get the same error. Thanks again Kyle!

  
#include "haarThread.h"  
#include "ofMain.h"  
#include <iostream>  
  
haarThread::haarThread()  
:ofThread()  
{  
    imgPath = "";  
    haarImg.setUseTexture(false);  
      
}  
  
void haarThread::setup(string haarFilePath) {  
    lock();  
      
    haarFind.setup(haarFilePath);  
      
    unlock();  
      
}  
  
void haarThread::loadImagePath(string filename) {  
    lock();  
      
    imgPath = filename;  
      
    unlock();  
}  
  
void haarThread::threadedFunction() {  
      
    while(true) {  
        //cout << "my own custom thread!" << endl;  
        //ofSleepMillis(3000);  
        haarImg.setUseTexture(false);  
        haarImg.loadImage(imgPath);  
        haarFind.findHaarObjects(haarImg);  
        faceRect = haarFind.blobs[0].boundingRect;  
          
  
    }  
  
}  

yes it seems like a bug in ofxCvHaarFinder, if you set use texture to false it shouldn’t be using opengl at all.