Mistake in AttributedString? (ArrayBase::checkSourceIsNotMember() assertion)


We have an attributed string and need to change its font:

attrStr.setFont (Range<int>(0, 3), Font (20.0f));

Unexpectedly, this line triggered the assertion in ArrayBase::checkSourceIsNotMember().

The comment on the assertion talks about passing a *copy rather than a *reference:

“When you pass a reference to an existing element into a method like add() which may need to reallocate the array to make more space, the incoming reference may be deleted indirectly during the reallocation operation! To work around this, make a local copy of the item you’re trying to add (and maybe use std::move to move it into the add() method to avoid any extra overhead.”

So, tracing the stack back to AttributedString::splitAttributeRanges (ArrayAttributedString::Attribute&, int), we noticed that a *reference is being passed rather than a *copy:

const AttributedString::Attribute& att = atts.getReference (i);
and then
atts.insert (i + 1, att);

Simply deleting the & on that first line solves our problem.

I have tried to create a minimal example but can’t reproduce the problem. It could well be that we’ve messed something else up to cause this. But in any case the fix that we’ve made *seems to honour the comment on the assertion better than the code that was already there.

Any thoughts on this? Thanks very much.


This has been fixed on the develop branch, see here JUCE 5.4.1 AttributedString color crash and here https://github.com/WeAreROLI/JUCE/commit/f1d5e5c9cc15d9b483868e766f56b4eb4947fdee