getFilter()->removeChangeListener(this);


#1

Hello

I am new to Juce, so I apologize if my question might be trivial.

I managed to exchange data between the processor and the editor (now the editor is updated whenever I need it, for instance when I call some preset files). However I read on the forum that the  "getFilter()->removeChangeListener(this); "  must be called in the editor destructor if the  "addChangeListener(this);"  has been used in the editor constructor?

I tried it, but I cannot find the getFilter() method? What is the proper way to do this?

Any advice would be appreciated. Thank you.
Max

 


#2

Are you looking for the getAudioProcessor() method perhaps?

http://www.juce.com/api/classAudioProcessorEditor.html#a4673c0d44d3c88bb0e8d1460a2224e3f


#3

Thanks. I found and tried geAudioProcessor()  (and processor, that should be the new way), but neither of the two seam to have the removeChangeListener() method. I found only removeListener().

By the way, before destroying the processor,  should I remove all the listeners I created (for the sliders, buttons etc.) or I  just have to call the removeChangeListener().

Thank you again.
Max

 


#4

If your AudioProcessor inherits the ChangeBroadcastor class then the removeChangeListener() method will be available to you. With regards to your second question I think you can just call removeAllChangeListeners().


#5

Thank you ver much, however I still cannot sort out the problem. Probably its a very trivial thing but I am a newbie.

I think that the AudioProcessor class was correctly declared:

class MyAudioProcessor  : public AudioProcessor,
                          public ChangeBroadcaster 

Then in the editor constructor I wrote:

MyAudioProcessorEditor::MyAudioProcessorEditor (MyAudioProcessor* ownerFilter)
    : AudioProcessorEditor(ownerFilter)
{

......

    // Register change listener after initialisation
    VolumeSlider->addListener(this);
    RightButton->addListener(this);
    LeftButton->addListener(this);
    ownerFilter->addChangeListener(this);
}

 

In the destructor i tried (with no success):

MyAudioProcessorEditor::~MyAudioProcessorEditor()
{
    if (VolumeSlider)
        VolumeSlider->removeListener(this);
    if (RightButton)
        RightButton->removeListener(this);
    if (LeftButton)
        LeftButton->removeListener(this);

    getAudioProcessor()->removeChangeListener();.

    deleteAllChildren();
}

 

But while ownerFilter in the constructor has the removeChangeListener() method, getAudioProcessor()-> does not.

By the way, I am confused about the "lifetime cycle" of a listener. Is the removeListener() always mandatory in the destructor?  To be clearer should I always call a removeListener() in the destructor if an addListener() call was present in the constructor?

Is there any way to make the above code cleaner and remove all the listeners that have been created (the ones associated with the slider, the buttons and the filter) at once?

Thank you again
Max


#6

There's actually no real need to remove listeners from a component that you're about to delete, because once deleted it no longer matters what listeners it had. I rarely bother calling removeListener in cases like this.

BTW please avoid deleteAllChildren() - using that method implies that you're not managing your object lifetimes with modern C++ techniques. From your code it look like these sliders should probably be just created as member variables, not pointers. And if they really really do need to be held as pointers for some reason that I can't see here, then you should use a ScopedPointer and not delete them manually. Maybe read my coding style guide!


#7

Thanks! However I still do not understand why the result of getAudioProcessor()  (or processor) is different from ownerFilter. Is this supposed to happen?



 


#8

getAudioProcessor returns the base class.

If you need a pointer to your own subclass of the object, you may want to add something like in the demo JuceDemoPluginAudioProcessorEditor::getProcessor()


#9

Thank you so much! Now it's clear.