Window proportions are incorrect in second window

Hi,

while working on an application with multiple windows I came across an odd issue. My basic setup is the same as in the multi window example: examples/events/multiWindowExample. I create two windows, two applications and I set a shared pointer in one application as a reference to the other.

ofGLFWWindowSettings appSettings;
appSettings.title = "Application";
appSettings.width = 1024;
appSettings.height = 768;
appSettings.resizable = true;
appSettings.setGLVersion(3, 3); // OpenGL 3.3 #version 330 – max support on my machine
shared_ptr<ofAppBaseWindow> appWindow = ofCreateWindow(appSettings);

ofGLFWWindowSettings controllerSettings;
controllerSettings.title = "Controller";
controllerSettings.width = 690;
controllerSettings.height = 800;
controllerSettings.resizable = false;
controllerSettings.setGLVersion(3, 3); // OpenGL 3.3 #version 330 – max support on my machine
shared_ptr<ofAppBaseWindow> controllerWindow = ofCreateWindow(controllerSettings);

shared_ptr<ofApp> app(new ofApp());
shared_ptr<ofController> controller(new ofController());

app->controller = controller;

So far, no problem. The issue happens once I call a method on the controller instance and within the method access the window size. For testing purposes I simply created the method foo() which logs the window size.

void ofController::foo() {
    ofLog() << "controller: " << ofGetWindowWidth() << "x" << ofGetWindowHeight();
}

The same log also happens in the update method. And this is what I get:

// when calling foo() [notice ] controller: 1024x768 // and right afterwards in the next update cycle [notice ] controller: 690x800

I tried to read up on shared pointers but so far I couldn’t find anything that explains this behavior. The way I would explain it right now is, that because the shared pointer of the ofController instance exists within the instance of ofApp, the scope of the application window is applied to the ofController instance for the time of executing foo(). If that is expected behavior, I really don’t know.

So far I came up with two solutions and one more option I didn’t test yet. The first solution is to simply set a boolean flag in foo() and do all the calculations in update() in the next cycle. The second option is to pass in a weak pointer to the instance of the controller window to the instance of ofController. This way I always have access to the current window properties. The third option would be to create a custom event in ofApp and subscribe a listener to it from ofController – of course need to adapt my setup of which app is holding the shared pointer to the other application. But I’m not sure whether this even possible, since I’m still getting used to custom events in OF.

My questions would be: First, is this expected behavior? If so, what are the recommended solutions to deal with the window scope issue? And are there any other issues to keep in mind when working with shared pointers across windows? And lastly, is it possible to subscribe listeners to events on shared pointer instances? Would this potentially also solve the problem?

Thanks for your help.

I ran a quick test with a custom event in ofController. Good news, events work across windows. Bad news, I ran into the same problem. Here’s my setup:

void ofApp::setup(){
    ofAddListener(controller->someEvent, this, &ofApp::onControllerEvent);
}

void ofApp::onControllerEvent() {
    ofLog() << "onControllerEvent(): " << ofGetWindowWidth() << "x" << ofGetWindowHeight();
}

Yielding the following output:

[notice ] onControllerEvent(): 690x800

I assume events happen synchronously which would explain the result. It also makes me think that this is expected behavior.