ValueTree - is it good idea to removeListener() inside valueTreeChildAdded()

Hello,
I have object that I want to be ValueTree::Listener, but I override only one method:
valueTreeChildAdded(ValueTree& parentTree, ValueTree& childWhichHasBeenAdded);

But after callback of that method I am sure my object doesn’t need to be listener anymore. So is it good idea to remove this listener inside valueTreeChildAdded(). It would look like that:

void valueTreeChildAdded(ValueTree& parentTree, ValueTree& childWhichHasBeenAdded)
{
    singletonWhichKeepsParentTreeWithThisListener::getInstance()->parentTree.removeListener(this);
    // I can't call directly input parameter parentTree.removeListener(this),
    // because that parentTree is of course the same ValueTree
    // but it is reference to another instance which
    // doesn't contain "this" listener

    // Here I do something with childWhichHasBeenAdded object
}

Above code works fine but I am not sure if it’s safe. Because valueTreeChildAdded is called from loop through all listeners, so when I remove listener here it is like I remove listener inside loop through all listeners.
It is difficult for me to debug it while there are some lambdas and references objects which I feel like I don’t understand. That’s why I would like ask you for advice.

For any help great thanks in advance.

I would have expected that to crash, for the reason you already mentioned (removing an element in the same loop you’re accessing it from). But if it works it works, I guess?

A safer solution might be to use AsyncUpdater to do the removing. You could put triggerAsyncUpdate() in the valueTreeChildAdded() function, an then do the listener removing inside handleAsyncUpdate().

Also, are you sure about that Singleton? A lot of folks around here frown on that sort of thing.