# Value and Listeners

**URL:** <https://forum.juce.com/t/value-and-listeners/13299>\
**Category:** General JUCE discussion\
**Created:** [August 4, 2014, 7:20pm UTC](https://forum.juce.com/t/value-and-listeners/13299 "2014-08-04T19:20:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [August 4, 2014, 7:20pm UTC](https://forum.juce.com/t/value-and-listeners/13299/1 "2014-08-04T19:20:50Z")

</div>

I Couldn't work out why my Value&nbsp;listener wasn't getting calle. But turns out that&nbsp;copying the Value object doesn't actually copy the listeners.&nbsp;(I was putting the listener on a value in a struct and then putting the struct in a std::vector).&nbsp;

Anyway - skimming the source it looks like the move operation on Value&nbsp;won't copy the listeners either.&nbsp;I assume that'll corrupt my stuff&nbsp;if my vector expands. &nbsp;

Though it's a bit irrelevant because changing the capacity on the vector makes ~Value() dereference a null pointer. An assert in ReferenceCountedObjectPtr triggers.&nbsp;I've not figured&nbsp;out quite why yet.

Anyway -&nbsp;I"m going to put the structure on the heap and have a a pointer to it to avoid all this. &nbsp;

I'm wondering if it'd be better if the Value couldn't be copied at all! &nbsp;Though bound to break stuff.&nbsp;

So big(ger) warnings in the&nbsp;documentation instead?&nbsp;

&nbsp;

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [August 5, 2014, 9:50am UTC](https://forum.juce.com/t/value-and-listeners/13299/2 "2014-08-05T09:50:46Z")

</div>

It's done that way deliberately - I think I originally wrote it to copy the listeners, but quickly realised that that caused all kinds of mayhem. I've used it in many different places, and found it's much better if listeners are only held locally.

A pattern I use a lot is this:

```
struct Foo : Value::Listener
{
    Foo (Value& v) : value (v) { value.addListener (this); }
    ~Foo() {} // no need to remove the listener

    Value value;
};
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [August 5, 2014, 11:49am UTC](https://forum.juce.com/t/value-and-listeners/13299/3 "2014-08-05T11:49:00Z")

</div>

I agree the local listener thing is great. &nbsp;I use exactly the same pattern everywhere. &nbsp;The problem is that Value allows a copy in a non-standard way which makes these two examples fail:&nbsp;

```
class Run :

public Value::Listener {

public:

    struct Entry {

        Value v;

    };

    

    Run() {

        /* This doesn't result in any listeners. */

        for (int i = 0; i<20; ++i) {

            Entry e;

            e.v = 100;

            e.v.addListener(this);

            data.push_back(e);

        }

        data.front().v = 101;

        /* This triggers an assert in ReferenceCountedObjectPtr. */

        for (int i = 0; i<50; ++i) {

            Entry d;

            d.v = 100;

            data.push_back(d);

            data.back().v.addListener(this);

        }

    }

    void valueChanged(Value & v) override {

        /* This is never reached. */

        DBG ("value changed to " + v.toString());

    }

private:

    std::vector<Entry> data;

};
```

The first one just fails silently and is a pain to spot what's happening. &nbsp;The second risks being a serious run-time bug because it occurs when the vector is resized, so won't show up with small data sets.&nbsp;

I'm trying to figure out exactly what's happening there...it's all gone a bit STL in the stack-trace!

---&nbsp;

EDIT: Looks like it's possible for

_&nbsp; &nbsp;&nbsp;if (listeners.size() \> 0)_

_&nbsp; &nbsp; &nbsp; &nbsp; value-\>valuesWithListeners.removeValue (this);_

**listeners.size()** to be non-zero but value to be **nullptr.**

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [August 5, 2014, 12:12pm UTC](https://forum.juce.com/t/value-and-listeners/13299/4 "2014-08-05T12:12:03Z")

</div>

> EDIT: Looks like it's possible for
> 
> &nbsp; &nbsp;&nbsp;if (listeners.size() \> 0)
> 
> &nbsp; &nbsp; &nbsp; &nbsp; value-\>valuesWithListeners.removeValue (this);
> 
> listeners.size()&nbsp;to be non-zero but value to be&nbsp;nullptr.

Maybe I'm being thick, but I can't see how that could happen? The value member can never be null.

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [August 5, 2014, 12:20pm UTC](https://forum.juce.com/t/value-and-listeners/13299/5 "2014-08-05T12:20:00Z")

</div>

Well, it's the referenceObject which is null...?&nbsp;

${ansi.fg.red}lldb$ ${ansi.normal}print value

(juce::ReferenceCountedObjectPtr\<juce::Value::ValueSource\>) $0 = {

&nbsp; referencedObject = 0x0000000000000000

}

I'll step through it and see if I can make sense of what's happening. &nbsp;Suspect it's a move thing. &nbsp;You use ReferenceCountedObject pointers move which sets the value to nullptr, then destructor of Value is called which still has listeners and tries to deference the value...

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [August 5, 2014, 12:27pm UTC](https://forum.juce.com/t/value-and-listeners/13299/6 "2014-08-05T12:27:28Z")

</div>

Yep. &nbsp;

```
{

            Value a;

            a.addListener(this);Value b = std::move(a);

        }
```

Crashes.

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [August 5, 2014, 12:29pm UTC](https://forum.juce.com/t/value-and-listeners/13299/7 "2014-08-05T12:29:00Z")

</div>

Suggest removing listeners during move. &nbsp;And add an assertion if it has listeners, because moving an object with listeners is probably very rarely wanted. &nbsp;That will go some way towards fixing my first problem of disappearing listeners.

&nbsp;

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [August 5, 2014, 12:46pm UTC](https://forum.juce.com/t/value-and-listeners/13299/8 "2014-08-05T12:46:39Z")

</div>

Yes, I was being thick!

Ok, try again now..

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [August 5, 2014, 1:07pm UTC](https://forum.juce.com/t/value-and-listeners/13299/9 "2014-08-05T13:07:46Z")

</div>

Cheers Jules. &nbsp;Update does not crash and comes up with the warning.&nbsp;

There's still a good run-time bug to be had if testing doesn't make the vector big enough to resize one of the elements with a listener, but it's a lot safer than it was!
