This is the location crash ocurrs:
and this is the callstack:
I guess the code below may cause it, which intended to create buttons dynamically:
H File
UCLASS()
class SURVIVEPLEASE_API UMultislotInventoryUserWidget : public UUserWidget
{
GENERATED_BODY()
public:
bool SetInventoryActor(AActor* inventoryActor);
void Clear();
private:
class UContainerComponent* CorrespondingContainerComponent;
TArray<class UButton*> Buttons;
public:
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Button")
TSubclassOf<UButton> ButtonStyleClass;
};
CPP File
bool UMultislotInventoryUserWidget::SetInventoryActor(AActor* inventoryActor)
{
Clear();
if (!inventoryActor) {
return false;
}
CorrespondingContainerComponent = Cast<UContainerComponent>(inventoryActor->GetComponentByClass(UContainerComponent::StaticClass()));
if (!CorrespondingContainerComponent) {
return false;
}
if (!WidgetTree) {
return false;
}
auto RootCanvas = WidgetTree->ConstructWidget<UCanvasPanel>();
auto ScrollBox = WidgetTree->ConstructWidget<UScrollBox>();
RootCanvas->AddChildToCanvas(ScrollBox);
UCanvasPanelSlot* ScrollBoxContainerSlot = Cast<UCanvasPanelSlot>(ScrollBox->Slot);
if (ScrollBoxContainerSlot) {
ScrollBoxContainerSlot->SetSize(FVector2D(768, 540));
ScrollBoxContainerSlot->SetPosition(FVector2D(1152, 0));
//ScrollBoxContainerSlot->SetAnchors(FAnchors(0, 0, 1, 1));
}
Buttons.SetNum(CorrespondingContainerComponent->Height * CorrespondingContainerComponent->Width);
for (UButton* button : Buttons) {
button = WidgetTree->ConstructWidget<UButton>(ButtonStyleClass);
ScrollBox->AddChild(button);
}
WidgetTree->RootWidget = RootCanvas;
return true;
}
Is this a bug or my mistake?
Thanks a lot!