Widget – segmentation fault

Hi!

I’m trying to build a widget on my module that graphically shows some parameters. First, I want to get the number of polyphony channels, but when I do this, I get a segmentation fault. I think I followed the example of the fundamental ADSR module: Fundamental/src/ADSR.cpp at v2 · VCVRack/Fundamental · GitHub

Could someone help me out?

Cheers!

struct Ad : Module {
...
  
  int nChannels = 1;

...
  
  Ad() {

...
  
  void process(const ProcessArgs &args) override {
    nChannels = max(inputs[VPOCT_INPUT].getChannels(), 1);

...

  }
};

struct SpectrumWidget : Widget {
  Ad* module;
  
  void drawLayer(const DrawArgs& args, int layer) override {
    if (layer == 1) { 
      if (module) {
        int nChannels module->nChannels;
      }
    }
    Widget::drawLayer(args, layer);
  }
};

struct AdWidget : ModuleWidget {
  AdWidget(Ad* module) {

...
    
    SpectrumWidget* spectrumWidget
      = createWidget<SpectrumWidget>(mm2px(Vec(.5*HP, 10.)));
    spectrumWidget->setSize(mm2px(Vec(PANELWIDTH-1.*HP,15.)));
    addChild(spectrumWidget);  
  }
};

Model* modelAd = createModel<Ad, AdWidget>("Ad");

what does that do? do you mean int nChannels = module->nChannels?

yes, sorry, I meant that

are you crashing in the browser, when module == nullptr?

Don’t know what I do with the second part of your question (beginner here), but the browser doesn’t crash.

ok. well, making you own widget it not a super easy thing to do. enjoy!

1 Like

the problem i think in your code is you never assign spectrum widgets module member

    SpectrumWidget* spectrumWidget
      = createWidget<SpectrumWidget>(mm2px(Vec(.5*HP, 10.)));
    spectrumWidget->setSize(mm2px(Vec(PANELWIDTH-1.*HP,15.)));

    // add this
    spectrumWidget->module = module;

    addChild(spectrumWidget); 

The other problem is you don’t initialize the module in your widget so if it is not assigned it will still be true since it will be random memory.


struct SpectrumWidget : Widget {
  Ad* module{nullptr};  // add that assignment
3 Likes

:see_no_evil: That’s it, it works! Thanks much! :pray:

And now I noticed the corresponding line in Fundamental/src/ADSR.cpp at v2 · VCVRack/Fundamental · GitHub .

I think I can submit the new version of my module to the library very soon.