# Best practice for storing text on the APVTS

**URL:** <https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291>\
**Category:** General JUCE discussion\
**Created:** [December 16, 2021, 4:46pm UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291 "2021-12-16T16:46:42Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![rafa1981](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@rafa1981](https://forum.juce.com/u/rafa1981)\
**Post date:** [December 16, 2021, 4:46pm UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/1 "2021-12-16T16:46:42Z")

</div>

I want to let the user edit each bus name on [MixMaxtrix](https://www.kvraudio.com/product/mixmaxtrix-by-artv) and to store it on the patch.

I have found this:

> <https://github.com/ffAudio/ffGuiAttachments/blob/master/ff_gui_attachments/ValueTreeLabelAttachment.h>

Is this method OK with regards to thread safety?

---

<div class="post-metadata">

**Author:** ![rafa1981](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@rafa1981](https://forum.juce.com/u/rafa1981)\
**Post date:** [December 16, 2021, 4:51pm UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/2 "2021-12-16T16:51:15Z")

</div>

This thread seems to be describing this approach as not thread unsafe.

> [@APVTS and state properties](https://forum.juce.com/t/apvts-and-state-properties/48637):
>
> I’m having an issue which seems random and not reproducible. I think I have the general direction but I’d like to know if this makes any sense at all or am I barking at the wrong tree here. tl;dr: is it safe to use apvts.state properties in real time like an AudioProcessorParameter sub tree? Description: my plugin has a welcome page and a dsp page, and I want to keep the selected page in the plugin state so I call apvts.state.setProperty(“selectedPage”,”dsp”,nullptr) or apvts.state.setPropert…

The author posted the solution but I don’t think I understand what he meant.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [December 16, 2021, 5:34pm UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/3 "2021-12-16T17:34:42Z")

</div>

> [@rafa1981](#):
>
> Is this method OK with regards to thread safety?

This is connecting components to a ValueTree property.  
Note that when I wrote that I wasn’t aware of the method to simply `referTo` a value.

```auto
ValueTree foo { "param" };
label.getTextValue().referTo (foo.getPropertyAsValue ("value", nullptr));

```

Which will already keep the value in the label synchronised with the property.  
If you use a property of the APVTS.state or ideally a sub-tree, that would already work.

Any string operation is never thread safe, as it cannot be copied in a single operation and hence not be wrapped into an atomic.

However to store a name the thread safety is not an issue. You don’t access that value from the audio thread, so it is fine. AFAIK it is made sure that calling setStateInformation() and getStateInformation() is either on the message thread (or has a MessageManagerLock?).  
Somebody please correct me if that’s wrong.

---

<div class="post-metadata">

**Author:** ![rafa1981](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@rafa1981](https://forum.juce.com/u/rafa1981)\
**Post date:** [December 16, 2021, 6:07pm UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/4 "2021-12-16T18:07:16Z")

</div>

Wow! referTo is extremely tidy, trying now… Thanks!

---

<div class="post-metadata">

**Author:** ![rafa1981](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@rafa1981](https://forum.juce.com/u/rafa1981)\
**Post date:** [December 16, 2021, 8:01pm UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/5 "2021-12-16T20:01:09Z")

</div>

It seemed to work.

I’m seeing this:

-When saving into APVTS.state I see that the values are stored and loaded from the XML when loading presets.  
-The labels are not refreshed on preset load. They return to default.  
-If I close and open the editor, then the labels texts are there correctly.

What I think that is happening is that the tree is rebuilt when loading, as I load like this:

> params.replaceState (juce::ValueTree::fromXml (\*state));

So …

> params.state-\>whatever\_it\_is\_implemented\_as\_value

… changes address. Leaving that callback/binding/whatever pointing to destructed objects I guess.

Does this have a solution?

---

<div class="post-metadata">

**Author:** ![rafa1981](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@rafa1981](https://forum.juce.com/u/rafa1981)\
**Post date:** [December 16, 2021, 8:05pm UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/6 "2021-12-16T20:05:41Z")

</div>

Does “referTo” implement “valueTreeRedirected” or similar?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [December 16, 2021, 8:43pm UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/7 "2021-12-16T20:43:46Z")

</div>

Unfortunately no. You need to reconnect when setStateInformation happens. That is the drawback.

---

<div class="post-metadata">

**Author:** ![rafa1981](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@rafa1981](https://forum.juce.com/u/rafa1981)\
**Post date:** [December 16, 2021, 9:07pm UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/8 "2021-12-16T21:07:24Z")

</div>

Then it looks that the original approach might be better. Thanks for your help!

---

<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:** [December 16, 2021, 9:46pm UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/9 "2021-12-16T21:46:31Z")

</div>

> [@daniel](#):
>
> AFAIK it is made sure that calling setStateInformation() and getStateInformation() is either on the message thread (or has a MessageManagerLock?).

I didn’t know that this is true. Is it? I assumed the host might do anything 😮

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [December 17, 2021, 9:05am UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/10 "2021-12-17T09:05:19Z")

</div>

AFAIK there is no guarantee about set/getStateInformation() being called on the message thread.  
Otherwise there wouldn’t be the need for discussions like this: [[AAX][PULL REQUEST] Force Protools to do chunk calls in message thread option](https://forum.juce.com/t/aax-pull-request-force-protools-to-do-chunk-calls-in-message-thread-option/40175)

From the discussion there (emphasis mine):

> [@\[AAX\]\[PULL REQUEST\] Force Protools to do chunk calls in message thread option](https://forum.juce.com/t/aax-pull-request-force-protools-to-do-chunk-calls-in-message-thread-option/40175/15):
>
> I’m not convinced it’s a good idea to add this feature. The docs for this feature flag say that it is supported “for now”, which implies that this might change in the future. **Also, other plugin formats do not make guarantees about which threads may call the get/set state functions**. A plugin that requires this feature will likely be broken when exported as an AU/VST3/etc, even if the issue is masked in the AAX version of the plugin. Ensuring your state functions are threadsafe will ensure that your plugin will work, even if the `AAX_eProperty_RequiresChunkCallsOnMainThread` feature is removed, or the plugin is exported in other formats.

---

<div class="post-metadata">

**Author:** ![rafa1981](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@rafa1981](https://forum.juce.com/u/rafa1981)\
**Post date:** [December 17, 2021, 9:51am UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/11 "2021-12-17T09:51:58Z")

</div>

I tried to use the valuetree listeners method again on APVTS.state and I don’t get any of the callbacks there invoked when calling:

> params.replaceState (juce::ValueTree::fromXml (\*state));

I could make a copy of the APVTS.state, then load the state from the xml and then manually do a full iteration restoring the listeners from the old copy of APVTS.state, but there is no way to retrieve the listeners of a var or value tree.

I’m trying hard to avoid the processor knowing the editor, but it is proving more difficult than I thought it would be.

---

<div class="post-metadata">

**Author:** ![rafa1981](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@rafa1981](https://forum.juce.com/u/rafa1981)\
**Post date:** [December 17, 2021, 3:02pm UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/12 "2021-12-17T15:02:33Z")

</div>

The “valueTreeRedirected” callback isn’t called either if I make a new children tree on APVTS.state and store my stuff there. Why?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [December 17, 2021, 3:08pm UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/13 "2021-12-17T15:08:32Z")

</div>

Depends on how you restore in setStateInformation.  
There is just assigning the new tree there, or copyPropertiesAndChildren and a few more methods involving manual things…

---

<div class="post-metadata">

**Author:** ![duvrin](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@duvrin](https://forum.juce.com/u/duvrin)\
**Post date:** [December 17, 2021, 7:32pm UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/14 "2021-12-17T19:32:11Z")

</div>

If I understand you correctly, then what you need to do is to reassign your text listeners in `ValueTree::Listener::valueTreeRedirected` which will be called automatically after you call `params.replaceState`.

Override `valueTreeRedirected` and repeat the same thing you do when you originally assign those listeners.

---

<div class="post-metadata">

**Author:** ![rafa1981](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@rafa1981](https://forum.juce.com/u/rafa1981)\
**Post date:** [December 17, 2021, 8:17pm UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/15 "2021-12-17T20:17:37Z")

</div>

I already tried both ways and neither calls “valueTreeRedirected” for some reason. With lots of attempts of different things, calling copyProperties, assignment operators, creating a children attached to APVTS.state etc.

The approach I’m trying now is to leave the APVTS on its own and to have a separate and stable ValueTree for the editor for the whole runtime. Then when saving the state i serialize both the APVTS and that other to XML and vice versa.

---

<div class="post-metadata">

**Author:** ![duvrin](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@duvrin](https://forum.juce.com/u/duvrin)\
**Post date:** [December 17, 2021, 8:30pm UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/16 "2021-12-17T20:30:16Z")

</div>

This is a good approach.  
Just going over the checklist - did you remember to call `apvts.state.addListener(this);` in the constructor of the object that listens to `apvts.state` properties?

---

<div class="post-metadata">

**Author:** ![rafa1981](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@rafa1981](https://forum.juce.com/u/rafa1981)\
**Post date:** [December 17, 2021, 8:56pm UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/17 "2021-12-17T20:56:56Z")

</div>

Yes. I’m using the ValueTreeLabelAttachment class from Daniel above. I’m sure it must be some dumb Juce newbie mistake somewhere.

---

<div class="post-metadata">

**Author:** ![duvrin](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@duvrin](https://forum.juce.com/u/duvrin)\
**Post date:** [December 17, 2021, 9:16pm UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/18 "2021-12-17T21:16:58Z")

</div>

If you listen to `APVTS.state`, then `valueTreeRedirected` must be triggered when calling `APVTS.replaceState()`.

If this doesn’t work for you, find the reason first. Don’t waste energy on the actual Values/properties, it won’t help.

`tree.toXmlString()` is your friend! Use it everywhere you can to detect when exactly your listener get detached. Put breakpoints in destructors and look closely at your subtrees.

---

<div class="post-metadata">

**Author:** ![rafa1981](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@rafa1981](https://forum.juce.com/u/rafa1981)\
**Post date:** [December 17, 2021, 9:25pm UTC](https://forum.juce.com/t/best-practice-for-storing-text-on-the-apvts/49291/19 "2021-12-17T21:25:00Z")

</div>

I have never ever seen a callback to valueTreeRedericeted. I have a printout there that never triggered. This is Linux Reaper.

Anyways, I got it working. For reference of future users, so they don’t spend as much time as me. “gui\_data” is a member of the processor that the processor never touches.

```auto
  void getStateInformation (juce::MemoryBlock& dst) override
  {
    using xml_ptr = std::unique_ptr<juce::XmlElement>;
    xml_ptr xml {apvts.copyState().createXml()};
    xml_ptr gui_xml {gui_data.createXml()};
    if (gui_xml) {
      auto edit = xml->createNewChildElement (gui_data.getType());
      *edit = *gui_xml;
    }
#if !defined(NDEBUG)
    printf ("Save preset\n%s\n", (char const*) xml->toString().toUTF8());
#endif
    copyXmlToBinary (*xml, dst);
  }

```

And for restoring, notice that I have a separate “set\_state” function because my plugin stores the builtin presets as a XML string on the binary, so it gets called from two places.

```auto
  //----------------------------------------------------------------------------
  void setStateInformation (void const* src, int src_bytes) override
  {
    set_state (getXmlFromBinary (src, src_bytes));
  }
  //----------------------------------------------------------------------------
  void set_state (std::unique_ptr<juce::XmlElement> xml)
  {
    if (!xml) {
      return;
    }
#if !defined(NDEBUG) && 0
    printf ("Set preset\n%s\n", (char const*) xml->toString().toUTF8());
#endif
    juce::XmlElement* gui = xml->getChildByName (gui_data.getType());
    if (gui) {
      // update expected values
      auto gui_new = juce::ValueTree::fromXml (*gui);
      for (int i = 0; i < gui_data.getNumProperties(); ++i) {
        auto key = gui_data.getPropertyName (i);
        auto prop = gui_new.getPropertyPointer (key);
        if (prop) {
          gui_data.setProperty (key, *prop, nullptr);
          gui_new.removeProperty (key, nullptr);
        }
        else {
          // not present means empty/default.
          gui_data.setProperty (key, "", nullptr);
        }
      }
      // keep unknown values on the preset, so an old version doesn't destroy
      // new presets.
      for (int i = 0; i < gui_new.getNumProperties(); ++i) {
        auto key = gui_new.getPropertyName (i);
        gui_data.setProperty (key, gui_new.getProperty (key), nullptr);
      }
      xml->removeChildElement (gui, true);
    }
    else {
      // clear everything
      for (int i = 0; i < gui_data.getNumProperties(); ++i) {
        gui_data.setProperty (gui_data.getPropertyName (i), "", nullptr);
      }
    }
    if (xml->hasTagName (apvts.state.getType())) {
      params.replaceState (juce::ValueTree::fromXml (*xml));
    }
    preset_was_loaded (std::move (xml));
  }

```

Thanks for the help!
