Chowdhury-DSP / chowdsp_utils

JUCE module with utilities for ChowDSP
Other
236 stars 24 forks source link

Safe way to remove Parameter Listeners #555

Open davispolito opened 1 week ago

davispolito commented 1 week ago

Is there a safe way to remove parameter Listeners? Like adding some sort of tag to the function? I am doing dynamic popup windows for the sliders but it seems as though the parameterListeners stick around even when the old sliders are deleted. Not 100% sure that this is what is happening yet but it looks like it

davispolito commented 1 week ago

My current workaround is to just simply not delete the c PopupComponent which holds a ParameterView once it's been created once. This fixes it. But I worry about memory whenever there are large numbers of objects that have been opened and closed and we don't care about them at any given moment they aren't on

jatinchowdhury18 commented 1 week ago

Usually I make a parameter listener by doing something like:

callback = pluginState.addParameterListener (...);

where callback is a chowdsp::ScopedCallback. The parameter listener will effectively be "deleted" when the callback is deleted. Usually, the simplest way to do this in your UI code is to have the callback object be a member variable in your Component class, so that the listener is deleted when the component is deleted. For example:

class MyComponent : public juce::Component
{
    chowdsp::ScopedCallback callback;

    MyComponent (chowdsp::PluginState& state)
    {
        callback = state.addParameterListener (...);
    }
    ~MyComponent() override = default; // the listener is deleted here!
};

If you need more granular control, you could store the callback in a std::optional<chowdsp::ScopedCallback> and manually reset() the optional when you want to delete the callback. Or if you have a bunch of them, it might be better to have a std::vector<chowdsp::ScopedCallback> and add/remove listeners from the vector as they are created/deleted.