[solved] dragLasso() crash when trying to build a SelectedItemSet

I’m trying to implement Lasso selection, but I keep getting a nasty runtime error on juce_HeapBlock.h (line 292). It happens when dragLasso() tries to copy one SelectedItemSet to another here:

  //juce_LassoComponent.h line
 void dragLasso (const MouseEvent& e)
 {
 // ...
     source->getLassoSelection() = SelectedItemSet<SelectableItemType> (itemsInLasso); // This copy fails (line 170)

I’ve followed the debugger and I’m pretty sure that I’m not passing any null or dangling pointers through. Here are the relevant extracts of my code:

SelectedItemSet<StandardSquareCell*>& CellBase::getLassoSelection()
{
    auto arrayOfSelectedCell = m_parentTabContent->getSelectedCells();
    jassert(arrayOfSelectedCell.getNumSelected() >= 0); // Just doing this to make sure that arrayOfSelectedCells is not null.

    return arrayOfSelectedCell;
}

void CellBase::findLassoItemsInArea(Array<CellBase*>& itemsFound, const Rectangle<int>& area)
{
    itemsFound.clear();

    for (auto* child : getChildren())
    {
        jassert(dynamic_cast<const CellBase*>(child)); // Making sure that 'child' is a valid CellBase*

        if (area.intersectRectangle(child->getBounds()))
            itemsFound.add(static_cast <CellBase*> (child));

    }
}

The CellBase* is added to the Array fine, but when it comes to be copied by dragLasso(), it crashes.

I thought that the problem might have something to do with the cast that I’m doing at the end, but I got the same result when I bypassed the cast using SelectedItemSet<Component*>.

Another hypothesis is that the template parameter is not being recognized by the Array because of some optimization process. I thought this because sometimes instead of the the HeapBlock error, it complains instead that it can’t find the virtual function for CellBase::itemSelected(). But this is just a guess, and it’s probably something else.

Can anyone see what I’m doing wrong?

Inside getLassoSelection(), I was returning a reference that was going out of scope. A novice mistake!