# ValueTree Listeners and Move-Ctor

**URL:** <https://forum.juce.com/t/valuetree-listeners-and-move-ctor/54033>\
**Category:** General JUCE discussion\
**Created:** [December 6, 2022, 4:55pm UTC](https://forum.juce.com/t/valuetree-listeners-and-move-ctor/54033 "2022-12-06T16:55:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rincewind](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rincewind/32/10645_2.png) [@Rincewind](https://forum.juce.com/u/Rincewind)\
**Post date:** [December 6, 2022, 4:55pm UTC](https://forum.juce.com/t/valuetree-listeners-and-move-ctor/54033/1 "2022-12-06T16:55:22Z")

</div>

Hi everyone,  
I’d be very glad if someone could give me a fresh perspective if I’m doing something wrong with my ValueTrees or if I’m expecting the correct behaviour from JUCE but it doesn’t deliver.

```nohighlight
- (void) test_valueTree_listener_base_in_vector
{
  DECLARE_Spy_Listener_Explicit(ValueTree::Listener, valueTreePropertyChanged) listener;
  
  std::vector<ValueTree> valueTrees;
  valueTrees.push_back(ValueTree("TEST"));
  valueTrees[0].addListener(&listener);
  
  valueTrees.resize(10);
  valueTrees[0].setProperty("MyProp", "MyVal", nullptr);
  XCAssertTrue(listener.__methodWasCalled);
}

```

The above test case is failing. Even though I’m attaching my ValueTree listener to the _reference_ in the std::vector, I’m not receiving the callback when the property changes. The problematic line is `valueTrees.resize(10);`. By that, the std::vector is being resized and the ValueTree objects are recreated. Since the move-ctor is marked noexcept, this one should be used. The resize statement for the vector is just a placeholder for any size changing operation. Adding/Removing elements from the vector that trigger the internal resize of the allocated RAM are all triggering this behaviour.

To fix this, the ListenerList would have to be moved into the new object. Is that something, that is recommended to be changed in JUCE or does that have side affects, that I don’t have the foresight for?

Best, Rincewind

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [December 6, 2022, 7:24pm UTC](https://forum.juce.com/t/valuetree-listeners-and-move-ctor/54033/2 "2022-12-06T19:24:51Z")

</div>

I think this is expected behaviour, and is consistent with the docs for addListener:

> <https://github.com/juce-framework/JUCE/blob/bbd6ccbc863edec3150e1e024e4fa3f636802596/modules/juce_data_structures/values/juce_ValueTree.h#L538-L551>

---

<div class="post-metadata">

**Author:** ![Rincewind](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rincewind/32/10645_2.png) [@Rincewind](https://forum.juce.com/u/Rincewind)\
**Post date:** [December 6, 2022, 7:43pm UTC](https://forum.juce.com/t/valuetree-listeners-and-move-ctor/54033/3 "2022-12-06T19:43:08Z")

</div>

Thanks for getting back to me! If that is the intended behavior, how would you suggest I implement a list like above? I think the main problem is, that this is allowed. Is it possible to generate a compiler error for using ValueTree in SRD::vector? It took me some time to figure out, why this is not working.

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [December 6, 2022, 7:53pm UTC](https://forum.juce.com/t/valuetree-listeners-and-move-ctor/54033/4 "2022-12-06T19:53:20Z")

</div>

> [@Rincewind](#):
>
> how would you suggest I implement a list like above

You could use `std::vector<std::unique_ptr<ValueTree>>`, or `std::list<ValueTree>`, both of which won’t attempt to move ValueTree instances when the container is resized.

---

<div class="post-metadata">

**Author:** ![Rincewind](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rincewind/32/10645_2.png) [@Rincewind](https://forum.juce.com/u/Rincewind)\
**Post date:** [December 6, 2022, 8:33pm UTC](https://forum.juce.com/t/valuetree-listeners-and-move-ctor/54033/5 "2022-12-06T20:33:57Z")

</div>

Thanks, great tip!!!
