Segfault when setting label text from focusLost()


#1

I’m using JUCE 2.0.40-33-g5d353d4 in Ubuntu 13.04. I have a class that publicly inherits from Label. In my virtual void focusLost( FocusChangeType cause ) method, I run some sanity checks on the content of the label, and reformat the text when necessary. This causes a segfault in JUCE.

A simplified example of what I’m trying to do with this: if the user types 10 digits into a phone number label, I want to reformat those 10 digits into a North American “phone number” format, such as (###) ###-####, once the user moves keyboard focus to a new field.

I’ve reproduced the problem with a very simple test case, which I’ll attach here. Strangely enough, even though my code is in focusLost(), I’m seeing the segfault when I first click on the label to give it focus.

The segfault is in Label::showEditor() at juce_Label.cpp line 212. The previous line 211 results in “editor” getting set to null, and then line 212 attempts to dereference the editor pointer.


#2

i pasted your example into my app and it crashed as you say. perhaps the Label class doesn’t expect the text to change after the focus is lost. could be a bug?


#3

Sounds like there’s some kind of focus gain/loss loop being invoked by whatever it is you’re doing. I’ll add some checking in case the editor object gets deleted there.


#4

No loop. See the very simple example code I included.

In playing around with it some more, I think the label gets the focus, then it creates the texteditor component, and the label loses the focus when it gives it to the texteditor.

I worked around it by adding this to my focusLost() method:

if ( hasKeyboardFocus(true) == false )
{
    ...

This way, the editor having gained focus doesn’t trigger my code which attempts to reformat the label’s text.


#5

Ok. I also added a check that means it should work without crashing now, even if the editor is deleted.