Help with ToolbarButtons


#1

Hi there,

Im pretty much a beginner with JUCE and C++, but over the last 3 months I have been developing an audio application to extract different sources from stereo audio files.  I'm implementing a media player element with a toolbar, but my ToolbarButton items are causing problems when I shutdown the application. My buttons are ScopedPointer<ToolbarButton> in my ToolbarFactory class and I initialise them in the constructor's initializer list. The order in which they are getting deleted is causing problems as it's trying to delete an object that has already been deleted (presumably because the buttons are ScopedPointers?).

Anyway, I just need some more experienced opinions on the matter. Should I make the buttons regular ToolbarButton objects? Is there some reason not to used scopedPointers in this instance?

Any help is appreciated :)


#ifndef TOOLBAR_H_INCLUDED
#define TOOLBAR_H_INCLUDED
#include "../JuceLibraryCode/JuceHeader.h"

class ToolbarFactory : public ToolbarItemFactory
{
public:
    enum itemIds
    {
        rewind = 1,
        play = 2,
        pause = 3,
        stop = 4,
        fForward = 5
    };
    ToolbarFactory() 
        : 
        rewindButton(new ToolbarButton(1, "Rewind", Drawable::createFromImageData(BinaryData::rewind_png, BinaryData::rewind_pngSize), 0)),
        playButton(new ToolbarButton(2, "Play", Drawable::createFromImageData(BinaryData::play_png, BinaryData::play_pngSize), 0)),
        pauseButton(new ToolbarButton(3, "Pause", Drawable::createFromImageData(BinaryData::pause_png, BinaryData::pause_pngSize), 0)),
        stopButton(new ToolbarButton(4, "Stop", Drawable::createFromImageData(BinaryData::stop_png, BinaryData::stop_pngSize), 0)),
        forwardButton(new ToolbarButton(5, "Fast Forward", Drawable::createFromImageData(BinaryData::ff_png, BinaryData::ff_pngSize), 0))
    {
    }
    ~ToolbarFactory()
    {
        rewindButton = nullptr;
        playButton = nullptr;
        pauseButton = nullptr;
        stopButton = nullptr;
        forwardButton = nullptr;
    }
    void getAllToolbarItemIds(Array<int>& ids) override
    {
        // This returns the complete list of all item IDs that are allowed to
        // go in our toolbar. Any items you might want to add must be listed here. The
        // order in which they are listed will be used by the toolbar customisation panel.
        ids.add(rewind);
        ids.add(separatorBarId);
        ids.add(play);
        ids.add(separatorBarId);
        ids.add(pause);
        ids.add(separatorBarId);
        ids.add(stop);
        ids.add(separatorBarId);
        ids.add(fForward);
    }
    void getDefaultItemSet(Array<int>& ids) override
    {
        // This returns an ordered list of the set of items that make up a
        // toolbar's default set. Not all items need to be on this list, and
        // items can appear multiple times (e.g. the separators used here).
        ids.add(rewind);
        ids.add(separatorBarId);
        ids.add(play);
        ids.add(separatorBarId);
        ids.add(pause);
        ids.add(separatorBarId);
        ids.add(stop);
        ids.add(separatorBarId);
        ids.add(fForward);
    }
    ToolbarItemComponent* createItem(int itemId) override
    {
        switch (itemId)
        {
        case rewind:
            return rewindButton;
            break;
        case play:
            return playButton;
            break;
        case pause:
            return pauseButton;
            break;
        case stop:
            return stopButton;
            break;
        case fForward:
            return forwardButton;
            break;
        default:
            break;
        }
    }
private:
    ScopedPointer<ToolbarButton> rewindButton, playButton, pauseButton, stopButton, forwardButton;
};
#endif  // TOOLBAR_H_INCLUDED

Thanks


#2

Fixed now. I'm not storing the ToolbarButtons in the ToolbarFactory anymore, just returning them to the toolbar in the main component.