Trying to create SvgSwitch where module can change momentary

I posted an issue yesterday, that I quickly closed again as I though I had fixed my issue, as it loaded my rack with the module just fine. However today I realized, it can open my rack with the module already inserted, however if I try to open the browser to insert other modules it crashes (hence not fixed). Here are the last lines of the log, but it doesn’t help me much

[7.415 info src/app/Browser.cpp:201 createPreview] Creating module widget Infinite-Noise PatchBay
[7.759 fatal adapters/standalone.cpp:49 fatalSignalHandler] Fatal signal 11. Stack trace:
31:  0x0
30:  0x0
29: _C_specific_handler 0x7ffaf940b1b0
28: _chkstk 0x7ffaf9c34f20
27: RtlFindCharInUnicodeString 0x7ffaf9baddd0
26: KiUserExceptionDispatcher 0x7ffaf9c34010
25: ZN20PatchBayModuleWidgetC1EP14PatchBayModule 0x7ffa43606f70
24: ZZN4rack11createModelI14PatchBayModule20PatchBayModuleWidgetEEPNS_6plugin5ModelENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEN6TModel18createModuleWidgetEPNS_6engine6ModuleE 0x7ffa43627f30
23: ZN4rack3app7browser8ModelBox4drawERKNS_6widget6Widget8DrawArgsE 0x7ffa4bcb0660
22: ZN4rack6widget6Widget9drawChildEPS1_RKNS1_8DrawArgsEi 0x7ffa4b852d00
21: ZN4rack6widget6Widget4drawERKNS1_8DrawArgsE 0x7ffa4b852e70
20: ZN4rack6widget6Widget9drawChildEPS1_RKNS1_8DrawArgsEi 0x7ffa4b852d00
19: ZN4rack6widget6Widget4drawERKNS1_8DrawArgsE 0x7ffa4b852e70
18: ZN4rack6widget6Widget9drawChildEPS1_RKNS1_8DrawArgsEi 0x7ffa4b852d00
17: ZN4rack6widget6Widget4drawERKNS1_8DrawArgsE 0x7ffa4b852e70
16: ZN4rack6widget6Widget9drawChildEPS1_RKNS1_8DrawArgsEi 0x7ffa4b852d00
15: ZN4rack6widget6Widget4drawERKNS1_8DrawArgsE 0x7ffa4b852e70
14: ZN4rack2ui12ScrollWidget4drawERKNS_6widget6Widget8DrawArgsE 0x7ffa4b84af60
13: ZN4rack6widget6Widget9drawChildEPS1_RKNS1_8DrawArgsEi 0x7ffa4b852d00
12: ZN4rack6widget6Widget4drawERKNS1_8DrawArgsE 0x7ffa4b852e70
11: ZN4rack6widget6Widget9drawChildEPS1_RKNS1_8DrawArgsEi 0x7ffa4b852d00
10: ZN4rack6widget6Widget4drawERKNS1_8DrawArgsE 0x7ffa4b852e70
9: ZN4rack6widget6Widget9drawChildEPS1_RKNS1_8DrawArgsEi 0x7ffa4b852d00
8: ZN4rack6widget6Widget4drawERKNS1_8DrawArgsE 0x7ffa4b852e70
7: ZN4rack6window6Window4stepEv 0x7ffa4b854f20
6: ZN4rack6window6Window3runEv 0x7ffa4b855a10
5: ZN4rack6window6Window3runEv 0x7ffa4b855a10
4: ZN4rack6window6Window3runEv 0x7ffa4b855a10
3: ZN4rack6window6Window3runEv 0x7ffa4b855a10
2: ZN4rack6window6Window3runEv 0x7ffa4b855a10
1: BaseThreadInitThunk 0x7ffaf8b92560
0: RtlUserThreadStart 0x7ffaf9beaf00

I am trying to create a “custom” SvgSwitch, with my own two svg-files. If I do the following in my widget, all is fine:

SvgSwitch* muteSwitch = createParam<SvgSwitch>(Vec(butCol1, butRow), module, PatchBayModule::MUTE1_PARAM + i);
muteSwitch->addFrame(APP->window->loadSvg(asset::plugin(pluginInstance, "res/InfNoiseLtSmallBlackButton.svg")));
muteSwitch->addFrame(APP->window->loadSvg(asset::plugin(pluginInstance, "res/InfNoiseLtSmallRedButton.svg")));
muteSwitch->momentary = false;

However I would like my module to “have access” to this switch, as I in some cases will need to change the “momentary” state of these switches, So in my module I have added the following array

SvgSwitch* muteSwitch[16];

And then in stead in my widget I do this

module->muteSwitch[i] = createParam<SvgSwitch>(Vec(butCol1, butRow), module, PatchBayModule::MUTE1_PARAM + i);
module->muteSwitch[i]->addFrame(APP->window->loadSvg(asset::plugin(pluginInstance, "res/InfNoiseLtSmallBlackButton.svg")));
module->muteSwitch[i]->addFrame(APP->window->loadSvg(asset::plugin(pluginInstance, "res/InfNoiseLtSmallRedButton.svg")));
module->muteSwitch[i]->momentary = false;

This last one is not working … most likely something trivial I can’t spot. As explained in the top, the odd part is it loads the rack just fine (with one of these modules already there), however it crashes when I try to open the browser

Are you checking if the module exists before running this block of code? When your ModuleWidget runs in the browser, there is no module

if(module) {
// then do all that stuff

This change might be enough to fix it, but I’ll say the pattern I usually see for this would be to keep the array muteSwitch as a member of your ModuleWidget, not of the Module. You’d have the Module keep its own array of boolean muteSwitchIsMomentary[16] values. And finally, override the step() method of ModuleWidget to synchronize the SvgSwitch ParamWidgets with the state in the module. I do something similar in a module Computerscare Boly Puttons, you can see the code here: computerscare-vcv-modules/src/ComputerscareBolyPuttons.cpp at master · freddyz/computerscare-vcv-modules · GitHub


Thanks a million, that was the root if the issue (not checking module)

EDIT: I was too eager to test if “if(module)” solved the issue (and it did). I now read the last of your anwer :slight_smile: Thanks for your suggestion I go have a look at the source. What you suggest sounds like a good idea.

1 Like

we’ve all been bitten by null module in the browser ;-). And the other advice from @computerscare is right on the money - you don’t want to be doing gui stuff from your process call.


Thanks to the both of you. @Squinky I probably NEEDED to be bitten by that issue myself. That way there is a higher chance I will remember it if/when I ever see it again. AFTER reading the answer from @computerscare, I remembered I had read about it somewhere before, but in the momemnt I forgot all about it, and my thoughts were elsewhere. I am used to all the comfort that Visual Studio and C# brings (probably also due to my experience), whereas when it comes to VS Code, C++ and having to “type commands”, just to build, I still feel like “Bambi on the ice” … but slowly various things becomes more easy, and I’m starting to see various patterns, like if I see X happen, I know it can be caused by Y, as I’ve had that issue before. … sloooowly getting there … small steps

Actually the feature with the module being able to alter the “momentary” of the switch is not needed for this module. Its something I know I need in another module, so while developing this module I decided to have a go at it, and see if I could get it working. I had a look at the source that @computerscare linked to, and I can see that is the “right way” to do it, whereas what I was trying to do, is more “a hack”. So going forward. thats the way I am going to implement it. Nice to have a great community to learn from.

1 Like

I’ve actually put things in the library that had that browser crash. Luckily VCV team realized the badness and rolled back my stuff in the library. That was years ago, but I haven’t repeated it, at least in public.

1 Like