webImageLoader inside of ofxThread

So, I realize you can’t run OpenGL inside of an ofxThread, but I’m having trouble just loading an image with webImageLoader in the threadedFunction. I’m not sure why this would be, but can you not use Poco commands in a thread (i’m using 057)? I’m setting “setUseTexture(false)”. Is there something else I need to do? I just need to load the pixels regularly from an IP camera, then use the pixel values in my main thread.


Here’s the thread I’m running:

#include "ofMain.h"  
#include "ofAddons.h"  
#include "webImageLoader.h"  
// this is not a very exciting example yet   
// but ofThread provides the basis for ofNetwork and other   
// operations that require threading.  
// please be careful - threading problems are notoriously hard   
// to debug and working with threads can be quite difficult  
class threadedObject : public ofxThread{  
	webImageLoader img;  
	int count;  // threaded fucntions that share data need to use lock (mutex)  
	// and unlock in order to write to that data  
	// otherwise it's possible to get crashes.  
	// also no opengl specific stuff will work in a thread...  
	// threads can't create textures, or draw stuff on the screen  
	// since opengl is single thread safe  
		count = 0;  
	void start(){  
		startThread(true, false);   // blocking, verbose  
	void stop(){  
	void threadedFunction(){  
		while( isThreadRunning() != 0 ){  
			if( lock() ){  
				if(count > 50000) count = 0;  
				ofSleepMillis(1 * 1000);  
	void draw(){  
		string str = "I am a slowly increasing thread. \nmy current count is: ";  
		if( lock() ){  
			str += ofToString(count);  
			str = "can't lock!\neither an error\nor the thread has stopped";  
		ofDrawBitmapString(str, 50, 56);  

And here’s the webImageLoader:

#include "webImageLoader.h"  
void webImageLoader::loadFromUrl(string url){  
   //poco is not happy if we register the factory more than once  
      factoryLoaded = true;  
   //specify out url and open stream  
   URI uri(url);        
   std::auto_ptr<std::istream> pStr(URIStreamOpener::defaultOpener().open(uri));  
   //copy to our string  
   string str;         
   StreamCopier::copyToString(*pStr.get(), str);  
   //figure out how many bytes the image is and allocate  
   int bytesToRead = str.size();  
   char buff[bytesToRead];  
   memset(buff, 0, bytesToRead);  
   //copy the bytes from the string to our buffer  
   for(int i = 0; i < bytesToRead; i++){  
    buff[i] = str[i];  
   printf("numBytes copied is %i \n", sizeof(buff));  
   //if we already have a loaded image clear it  
  // if(isValid()){  
  // }  
   //create a freeimage memory handle from the buffer address  
   FIMEMORY *hmem = NULL;  
   hmem = FreeImage_OpenMemory((BYTE *)buff, bytesToRead);  
   if (hmem == NULL){ printf("couldn't create memory handle! \n"); return; }  
   //get the file type!  
   FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem);  
   //make the image!!  
	putBmpIntoPixels(FreeImage_LoadFromMemory(fif, hmem, 0), myPixels);  
 //  bmp = FreeImage_LoadFromMemory(fif, hmem, 0);  
   //free our memory  
   if (getBmpFromPixels(myPixels) == NULL){ printf("couldn't create bmp! \n"); return; }  
   //flip it!  
	if (myPixels.bAllocated == true && bUseTexture == true){  
		tex.allocate(myPixels.width, myPixels.height, myPixels.glDataType);  

what is the error you are getting?

one thing that won’t work is this code:

   if (myPixels.bAllocated == true && bUseTexture == true){  
      tex.allocate(myPixels.width, myPixels.height, myPixels.glDataType);  

because that is doing openGL stuff - so you would have to make sure that this is not in the threaded function.

hi Matt,

could you be more exact about what kind of problems you’re having? it’s very difficult to tell what’s wrong just by looking at code…


Hey guys,

Sorry I wasn’t more specific.

The program hangs when I try to load the image from the URL. I eventually have to kill it.

Theo, I don’t think that code should be a problem since I’m calling setUseTexture(false), right? Or is that not what that does? Is there something else I need to call to disable that?