There are various other occasions but the one I just stumbled over is:
std::unique_ptr<Drawable> Drawable::createFromImageData (const void* data, const size_t numBytes)
This should be a shared pointer rather than a unique pointer: This function is creating an image on request so it’s obvious that the user wants to do something with the result. If its a unique_ptr she needs to take care about creating a copy by herself. If it was a shared_ptr it could just be further passed on.
It isn’t creating an image, it is creating a Drawable, which is a Component. Components are not ‘sharable’. They can only have one parent Component. What is the use case you were thinking of?
And generally, a std::shared_ptr
can always be initialized from a std::unique_ptr
. So if you need it to be shared, just do something like that
std::shared_ptr mySharedPtr = juce::Drawable::createFromImageData (BinaryData::myDrawable_svg, BinaryData::myDrawable_svgSize);
If you need to use the drawable from multiple places and you want to optimize its creation, I would use a pattern like this:
static inline std::unique_ptr<juce::Drawable> getDrawable()
{
static const auto d = juce::Drawable::createFromImageData (BinaryData::myDrawable_svg,
BinaryData::myDrawable_svgSize);
return d->createCopy();
}
1 Like