Freeing slots from SScrollbox causing editor crashes

I’ve been working on a mod to the editor which involves a Slate window consisting of a SScrollbox whose content is dynamically generated. Each scrollbox slot contains a SHorizontalBox with its slots containing STextBlocks. The user can modify/update the contents of the scrollbox by hitting a button.

I’m keeping a TArray of all the SHorizontalBox widgets so that I can remove the slot later on. A simplified version of the code looks like this:

TArray<TSharedPtr<SHorizontalBox>> LineItems;
TSharedPtr<SScrollBox> ScrollBox;

void MakeAScrollBox()
{
	SAssignNew(ScrollBox, SScrollBox)
		.Orientation(Orient_Horizontal)
		.ScrollBarAlwaysVisible(true);
}

void AddSomeContent()
{
	TSharedPtr<SHorizontalBox> HBox;

	for (int32 i = 0; i < 10; i++)
	{
		ScrollBox->AddSlot()
		[
			SAssignNew(HBox, SHorizontalBox)

			+ SHorizontalBox::Slot()
			[
				SNew(STextBlock)
				.Text(FString("Hello"))
			]

			+ SHorizontalBox::Slot()
			[
				SNew(STextBlock)
				.Text(FString("World"))
			]
		];

		LineItems.Add(HBox);
	}
}

void RemoveScrollBoxContent()
{
	for (int32 i = 0; i < LineItems.Num(); i++)
	{
		ScrollBox->RemoveSlot(LineItems[i].ToSharedRef());
	}

	LineItems.Empty(); // <------- will cause crash!?!?!
}

I need to clear the array that has the list of widgets to the scrollbox. But doing so causes the editor to crash with all sorts of odd memory errors. It seems the shared pointers are getting corrupted, but I don’t know why.