~TextEditor and Value::listener


I have discovered the concept of juce’s Value object just a couple of weeks ago (thanks to Jules’ answer to one of my threads) and found it incredibly powerful. I have been able to drastically reduce the size of my code, but I must say I do not quite fully get how it’s meant to be used.

I have come across an interesting problem. My application currently crashes on shutdown and I have tracked the problem down to TextEditor destructor. There’s this line:

textValue.referTo (Value());

Well. My problem is obvious and I know how to solve it but since I am at it, I’d like to know more about the philosophy of the Value::Listener(s).

Here the problem is that the line mentioned above causes all the listeners to be informed. In my case the listener no longer exists. I have all my components in some OwnedArray and in this case the Listener were positioned before the TextEditor so it was freed earlier. That is, if I swapped those two objects in the array it would help, but that is of course not the point. So there are two questions:

  1. Why is there the line mentioned above in the TextEditor destructor. What is it supposed to do?
  2. How are the Values and their Listeners supposed to work, do I always need to unregister all the listeners before deleting the object? I ask this because I have seen something called valuesWithListeners in the Value class and I don’t understand what is it for, but at first glance it seemed to me that it might have something to do with unregistering the listeners…



Well, I made a workaround but I don’t believe that’s the best solution. The listener now also remebers a pointer to the value and in it’s destructor there is a code that removes the listener from the Value listener list. But I believe there is a better solution for that in JUCE. Even Components are removed from their parents when they are deleted, so I’d expect something similar to be implemented here…