The documentation for
The button will keep its own internal copies of these drawables.
So you always dealt with hundreds of copies in your old code anyway If this has not been an issue before, it won’t be now.
Regarding the changes towards
std::unique_ptr: The old API expected that you deleted the returned drawable manually after using it, I expect that you have a
delete ButtonOnDrw somewhere after the code where you created all your buttons.
std::unique_ptr now takes care of memory management so that you simply cannot forget
delete calls on pointers (which creates memory leaks) but automatically deletes the pointed to object as soon as it goes out of scope. Before
std::unique_ptr was used as a de-facto standard in modern C++, JUCE already had the
ScopedPointer class that basically did the same (but is now deprecated in favour of
To retrieve the underlying raw pointer from a unique ptr, there is the
get function. You could rework your code like that:
IdShapeButton::IdShapeButton (const String& name, std::unique_ptr<Drawable>& Off, std::unique_ptr<Drawable>& Over, std::unique_ptr<Drawable>& On, std::unique_ptr<Drawable>& OverOn): DrawableButton (name, ImageStretched)
setImages (Off.get(), OverOn.get(), On.get(), nullptr, On.get(), Over.get(), On.get());
auto ButtonOnDrw = Drawable::createFromImageFile(Mon);
mute[channel] = new IdShapeButton("Mute", ButtonOnDrw, ButtonOverDrw, ButtonOffDrw, ButtonOvrOnDrw);
Note that I used
auto here, which avoids long typenames – a style that is especially beneficial when a lot of unique ptrs are used, as they create quite long typenames
Sidenote: Since you are allocating the buttons themselves also via new, you might also want to brush up that code to follow modern C++ patterns use unique ptrs (which of course requires that the container you are assigning the pointer to is suitable to unique pointers) e.g. like
mute[channel] = std::make_unique<IdShapeButton> ("Mute", ButtonOnDrw, ButtonOverDrw, ButtonOffDrw, ButtonOvrOnDrw);
This will avoid a lot of
delete calls in your destructors. But of course, your first aim is to get your project compiling at all