Force single module instance

Who is this referring to? I’ve a single struct at plugin scope to share theme data between modules. I assume Meander has tons of global stuff (at least for now) as it’s being ported from C.

Yeah, something like this pseudocode would be better. Not thread-safe, but I’ll leave that as an exercise…

struct MyModule : Module {
	static MyModule* singleton = NULL;

	~MyModule() {
		if (singleton == this)
			singleton = NULL;
	}
	void process() {
		if (!singleton)
			singleton = this;

		if (singleton != this) {
			outputs[...].setVoltage(0.f);
			return;
		}

		...
	}
};
1 Like

Exactly - and if you sub in std::atomic and the right use of compare exchange you should be good

@Squinky Yeah, Meander has a lot of globally scoped data since Meander for Windows is a C application and most of the guts for Meander for Rack was directly ported from the app.

I have this methodology all working in Meander now, but it took a lot of trial and error. Basically I had to code 3 “use cases”:

  • How the 1st instance panel should appear.
  • How the module add preview panel should appear.
  • How the subsequent added instances should appear.

Everything is functional again, including the preview instance with my ModuleWidget formatting the panel view, but without a module pointer. I definitely would not recommend doing this unless there is a good or necessary reason, but I’m glad it is working for me now. Maybe some day I will move all of the globally scoped data into the module, although that makes it difficult to have the ModuleWidget do any data based drawing on the panel when there is no module, such as in preview…

1 Like

Still, is it really multiple days work to move it from global scope to module scope where it belongs? I guess it must be a lot of work or you would have done that.

There are ~3000 lines of global types, variables and functions. Both the module and the ModuleWidget need to be able to access this data. The module process() has to be able to read and modify this data, based on user parameter controls. If I have done everything correctly, the ModuleWidget just needs to read this data and use it to render the panel. I’m still concerned that if I put everything at the module scope, can the ModuleWidget always access it, especially in preview mode where there is no module, or at least that is my understanding.

I’ve only been developing for Rack plugins for 3 months, so my understanding is still incomplete and potentially incorrect. Feel free to educate me. Would it be better to discuss this in its own topic?

Well, with 300 lines sounds like you made the right choice!

1 Like