ofTexture seems uninitialized when used from another thread

Hello,

I’ve been trying to add some graphics to one of my apps, and for that I was using ofImage, writing pixel data, then drawing it as a texture with ofImage::draw() After initialization of the ofImage the data stored in the ofTexture seems correct, but when I write the pixels in a separate worker thread the data reverts to 0xcdcdcd on all pixels, which I interpreted (probably falsely) as uninitialized. Since the problem was reproducible, here I present you with the test app:

ofApp.h:

class ofApp : public ofBaseApp{
public:
	void setup();
	void draw();
	
	GUI_Class* ui;
	ThreadedClass* thread;
};

ofApp.cpp:

void ofApp::setup(){
	thread = new ThreadedClass();
}

void ofApp::draw(){
	thread->Draw();
}

GUI_Class.h:

class GUI_Class
{
public:
	GUI_Class();

	void SetPixels(int seed);
	void Draw();

private:

	ofImage* Image;
};

GUI_Class.cpp:

GUI_Class::GUI_Class()
{
	ofPixels *p = new ofPixels();
	p->allocate(640, 640, ofImageType::OF_IMAGE_COLOR);
	Image = new ofImage(*p);
	delete p;
}

void GUI_Class::SetPixels(int seed)
{
	for (int y = 0; y < Image->getWidth(); y++)
	{
		for (int x = 0; x < Image->getHeight(); x++)
		{
			Image->setColor(x, y, ofColor((x + y + seed) % 255, (x - y - seed) % 255, (x * (y + seed)) % 255));
		}
	}
	Image->update();
}

void GUI_Class::Draw()
{
	Image->draw(0, 0, 640, 640);
}

ThreadedClass.h:

class ThreadedClass : public ofThread
{
public:
	ThreadedClass();

	void Draw();

private:
	void threadedFunction() override;

	GUI_Class* UI;
};

ThreadedClass.cpp:

ThreadedClass::ThreadedClass()
{
	//ofTexture is CORRECT
	UI = new GUI_Class();
	//ofTexture is CORRECT
	startThread();
}

void ThreadedClass::Draw()
{
	lock();
	UI->Draw();
	unlock();
}

void ThreadedClass::threadedFunction()
{
	//ofTexture is INCORRECT
	int i = 0;
	while (isThreadRunning())
	{
		lock();
		//ofTexture is INCORRECT
		UI->SetPixels(i);
		//ofTexture is INCORRECT
            i = (i + 1) % 640;
		unlock();
		ofSleepMillis(200);
	}
}

The texture drawn on the window is homogenous light grey, probably 0xcdcdcd, which you can test by dumping the texture withImage->getTexture().readToPixels() at the lines marked with “INCORRECT”.
If you modify the ofApp.cpp to

void ofApp::setup(){
	ui = new GUI_Class();
	ui->SetPixels(100);
}

void ofApp::draw(){
	ui->Draw();
}

everything works as intended. All through this the values in ofImage::pixels remains correct
Has anyone encountered this before, or has any idea/suggestion?

opengl objects can’t be used from different threads right away, you would need to create a second opengl context and avoid any OF calls that call any globals objects (any kind of bind/begin and draw) and instread do those through an explicit OF renderer call used only from that thread