Weird problem of disappearing GUI when adding a new label


#1

Hi, I've got this strange problem and any help at all would be amazing!

So I've created a custom Component that has a ComboBox and a label in it. I've added a bunch of these before with no problems, but then suddenly if I added just one more, the GUI stops working. The plugin compiles and loads fine with no errors, but the GUI is blank. Weirder still, if in the constructor of the custom component I set the label to to be empty, the GUI reappears.

Below is the constructor of the custom control. If I call label->setText("", dontSendNotification) instead, it works.

SelectorControl::SelectorControl(float x, float y, float w, const String& labelText, StringArray& items)
{
    setBounds(x, y, w, 40);
    Label* label;
    addAndMakeVisible(label = new Label());
    label->setBounds(0, 0, getWidth(), 20);
    label->setText(labelText, dontSendNotification);
    label->setJustificationType(Justification::topLeft);
    addAndMakeVisible(comboBox = new ComboBox());
    comboBox->setBounds(0, 20, getWidth(), 20);
    comboBox->addItemList(items, 1);
    comboBox->setSelectedId(1, false);
}

The whole GUI is based on an OpenGL component...so I don't know if that's part of the problem. 

It's hard to debug since there are no reported errors, and everything seems to initialise fine.

Demonstrating the addition of one more SelectorControl:

// in the .h file:
ScopedPointer<SelectorControl> filterTypeControl, midiRangeLowControl, midiRangeHighControl;


// if I put this it works fine
addAndMakeVisible(midiRangeLowControl = new SelectorControl(10, y, 80, "Note low", items));
addAndMakeVisible(midiRangeHighControl = new SelectorControl(100, y, 80, "Note high", items));

// but if I put this the GUI disappears, with no errors
addAndMakeVisible(filterTypeControl = new SelectorControl(10, y, 80, "Type", items));
addAndMakeVisible(midiRangeLowControl= new SelectorControl(10, y, 80, "Note low", items)); addAndMakeVisible(midiRangeHighControl = new SelectorControl(100, y, 80, "Note high", items));

I'm am literally at my wits end. What the F is going on?! When unexplainable things happen in c++ like this my first thought is "you've done something stupid", but I've checked again and again, and my second thought is some memory mess up is going on, like an array that's been written into past the end or whatnot. But this seems....different. Any ideas?

Cheers,

Rob

 

 


#2

You're label pointer is local to the constructor - make it a ScopedPointer and make it a member variable instead.


#3

Ahhh goddamn it. Components don't own their children. Components don't own their children. Components don't own their children. Components don't own their children. 

Thanks! Sometimes you just need someone to slap you in the face.

 


#4

Ok all isn't well. I'd had it like you'd suggested originally, but I'd changed it to the version above in desperation. The problem remains. I've narrowed it down.

There seems to be a limited number of times I can call setText before the GUI fails.

Another but related class to the one above looks like this:

In DialControl.h

ScopedPointer<Label> label;
    ScopedPointer<Label> valueLabel;
    ScopedPointer<Slider> slider;

In DialControl.cpp

DialControl::DialControl(float x, float y, float w, float h, const String& labelText) 
{
    setBounds(x, y, w, h);
    addAndMakeVisible(label = new Label());
    label->setBounds(0, 0, getWidth(), 20);
    label->setText(labelText, dontSendNotification);
    label->setJustificationType(Justification::centredTop);

    addAndMakeVisible(slider = new Slider());
    slider->setBounds(0, 20, getWidth(), getHeight() - 40);
    slider->setSliderStyle(Slider::SliderStyle::RotaryVerticalDrag);
    slider->setTextBoxStyle(Slider::NoTextBox, true, getWidth(), 20);
    slider->addListener(this);
    slider->addMouseListener(this, true);

    addAndMakeVisible(valueLabel = new Label());
    valueLabel->setText("123", dontSendNotification);
    valueLabel->setBounds(0, getHeight() - 20, getWidth(), 20);
    valueLabel->setJustificationType(Justification::centred);
}

If I set both label and valueLabel the GUI fails to initialise and I get a blank screen. I can have one or the other. If I remove instances of DialControl then I can set both labels. So it seems to be related to the number of times it's called.

!?!?!?!?!!?!!?!?!?!??


#5

No it literally is based on the number of calls to setText. I've put in a for loop that creates Labels. If I call setText more than a couple of time the GUI crashes. You don't need to addAndMakeVisible to the GUI to fail.

for(int i = 0; i < 10; i++)
{
    Label* label = new Label();
    label->setText("asdas", dontSendNotification);
}

If I comment out  textValue = newText in Label::setText then it works.

I am using large numbers of Value objects in this plugin. Thousands. Could it be related to that?


#6

It's unpredicitable. Sometimes it works and sometimes not, so it's probably a memory thing or maybe a threading thing. Any suggests for tracking it down? What are the usual suspects?


#7

Why isn't there an error being shown? How can I get it to crash properly so I at last get some idea of what the F is happening?