Shared objects between windows

Hello! im following the multi window example, creating two apps for two different windows.
How can i share one object (creating in the ofApp) to the other window? i want to (lets say, for example) click the second window and create a new Object in the main Window.

#include "ofMain.h"
#include "ofApp.h"
#include "GuiApp.h"
#include "ofAppGLFWWindow.h"
#include "radarApp.hpp"


//========================================================================
int main( ){
	ofGLFWWindowSettings settings;

    settings.setSize(600, 600);
	settings.setPosition(ofVec2f(300,0));
	settings.resizable = true;
	shared_ptr<ofAppBaseWindow> mainWindow = ofCreateWindow(settings);

    settings.setSize(300, 800);
	settings.setPosition(ofVec2f(0,0));
	settings.resizable = false;
	shared_ptr<ofAppBaseWindow> guiWindow = ofCreateWindow(settings);
    
    
    
    settings.setSize(500, 500);
    settings.setPosition(ofVec2f(0,0));
    settings.resizable = false;
    shared_ptr<ofAppBaseWindow> radarWindow = ofCreateWindow(settings);


	shared_ptr<ofApp> mainApp(new ofApp);
	shared_ptr<GuiApp> guiApp(new GuiApp);
    shared_ptr<radarApp> radarAppl(new radarApp);


	ofRunApp(guiWindow, guiApp);
	ofRunApp(mainWindow, mainApp);
    ofRunApp(radarWindow, radarAppl);
    
	ofRunMainLoop();

}

this is my main code. in mainAPP i create an object and i want to use it in the radarWindow.

hi. as those apps are all shared pointers you can have these shared between them. so you can have also a shared_ptr<radarApp> inside mainApp and make it point to the radarApp you are creating in main.cpp.

so, before you call ofRunApp you could add

mainApp->radarAppPtr = radarAppl;

which will give you access to the radarApp from within the mainApp. or you could do it the other way round.
It is important to notice that if you add a shared pointer in both clases and pointing to each other you can end with a circular reference which can lead to a memory leak. In this use case it will probably not be a problem, but it can be when you are dynamically creating and destroying these objects. This is easily solved by using a weak_ptr instead of a shared_ptr.

thanks. it works if i used the radarApp inside the mainApp, but not in the other way:

//
//  radarApp.hpp
//  multiWindowExample
//
//  Created by Juan Pedro Vallejo on 22/2/19.
//
//
#pragma once
#ifndef radarApp_hpp
#define radarApp_hpp

#include "FragmaApp.hpp"
#include "ofMain.h"

class radarApp: public ofBaseApp {
public:
    

    
    void setup();
    void update();
    void draw();
    void mousePressed(int x, int y, int button);
    
    shared_ptr<ofApp> m;


};

#endif /* radarApp_hpp */

the error is: Use of undeclared identifier “ofApp”

An easier way to avoid this problems is to have a separate class where you store the common variables to all the applications. For example something like State for the common state of all the windows. then share an instance of that with all the apps using a shared_ptr as in:

#include "ofMain.h"
#include "ofApp.h"
#include "GuiApp.h"
#include "ofAppGLFWWindow.h"
#include "radarApp.hpp"
#include "State.h"


//========================================================================
int main( ){
	ofGLFWWindowSettings settings;

    settings.setSize(600, 600);
	settings.setPosition(ofVec2f(300,0));
	settings.resizable = true;
	shared_ptr<ofAppBaseWindow> mainWindow = ofCreateWindow(settings);

    settings.setSize(300, 800);
	settings.setPosition(ofVec2f(0,0));
	settings.resizable = false;
	shared_ptr<ofAppBaseWindow> guiWindow = ofCreateWindow(settings);
    
    
    
    settings.setSize(500, 500);
    settings.setPosition(ofVec2f(0,0));
    settings.resizable = false;
    shared_ptr<ofAppBaseWindow> radarWindow = ofCreateWindow(settings);


	shared_ptr<ofApp> mainApp(new ofApp);
	shared_ptr<GuiApp> guiApp(new GuiApp);
    shared_ptr<radarApp> radarAppl(new radarApp);

      shared_ptr<State> sharedState(new State);
      mainApp->sharedState = sharedState;
      guiApp->sharedState = sharedState;
      radarAppl->sharedState = sharedState;

	ofRunApp(guiWindow, guiApp);
	ofRunApp(mainWindow, mainApp);
    ofRunApp(radarWindow, radarAppl);
    
	ofRunMainLoop();

}

State might just have all public variables so you can access them from anywhere

1 Like

why not?

i comment previously:
the error is: Use of undeclared identifier “ofApp”

That is because of recursive inclusions, you are including ofApp from radarApp and radarApp from ofApp which can’t be done directly in c++. you need to use forward declaration to be able to do it. look it up online or even in the forum, it’s been answered several times.

The pattern i posted before solves that and many other problems without need for forward declaration

1 Like