UWebBrowser Causes problems with GC


I am using UE 4.26 and have issue with UWebBrowser not being properly deleted.
We ale implementing all of this stuff in C++. Here is the thing:

We are trying to implement this:


I am spawning the actor in my Player controller:

FTransform T{};

FActorSpawnParameters P{};

TestScreenActor =
    Cast<ACSScreenActor>(GetWorld()->SpawnActor(ACSScreenActor::StaticClass(), &T, P));

Actors Constructor:

        PrimaryActorTick.bCanEverTick = true;


        // Create the widget component
        WebBrowserWidgetComponent = CreateDefaultSubobject<UWidgetComponent>("WebWidget");


        WebBrowserWidgetComponent->SetMaterial(0, MaterialInstanceObject);


Then comes the construction of the widget. I Inherit from UUserWidget and implement its rebuild widget and native construct as follows:

TSharedRef<SWidget> UCSScreenWidget::RebuildWidget()
    auto* RootWidget = Cast<UCanvasPanel>(GetRootWidget());

    if (nullptr == RootWidget)
        RootWidget =
                UCanvasPanel::StaticClass(), FName("Canvas"));
        UCanvasPanelSlot* RootSlot = Cast<UCanvasPanelSlot>(RootWidget->Slot);
        if (nullptr != RootSlot)
            RootSlot->SetAnchors({ 0.0, 0.0, 1.0, 1.0 });
            RootSlot->SetOffsets({ 0.0, 0.0 });
        WidgetTree->RootWidget = RootWidget;

    if (nullptr == RootWidget || nullptr == WidgetTree)
        return Super::RebuildWidget();

    RootWidget->SetRenderTransformPivot({ 0.5, 0.5 });

    WebBrowser =
            UWebBrowser::StaticClass(), FName("WebBrowser"));
    UCanvasPanelSlot* CanvasPanelSlot =

    if (nullptr == CanvasPanelSlot)
        UE_LOG(LogTemp, Error, TEXT("Resize failed"));
        return Super::RebuildWidget();

    CanvasPanelSlot->SetAnchors({ 0.0f, 0.0f, 0.0f, 0.0f });
    CanvasPanelSlot->SetPosition(FVector2D(0.f, 0.f));
    CanvasPanelSlot->SetAlignment({ 0.0, 0.0 });

    return Super::RebuildWidget();

void UCSScreenWidget::NativeConstruct()

    if (nullptr != WebBrowser)

The issue

The issue is that when i load any URL ( the component has to not be hidden and has to be visible ) it does not get properly cleaned up. What i mean by that is i get an exception from here:

// This can happen during blueprint compiling, so just ignore it if it happens then, this is really only a concern
// in a running game.
if (!GCompilingBlueprint && !GIsGCingAfterBlueprintCompile)
	// This is only a concern during a running game - design-time instances can be destroyed from GC quite often when recompiling
	if (!WidgetObject || !WidgetObject->IsDesignTime())
		ensureMsgf(!IsGarbageCollecting(), TEXT("SObjectWidget for '%s' destroyed while collecting garbage.  This can lead to multiple GCs being required to cleanup the object.  Possible causes might be,\n1) ReleaseSlateResources not being implemented for the owner of this pointer.\n2) You may just be holding onto some slate pointers on an actor that don't get reset until the actor is Garbage Collected.  You should avoid doing this, and instead reset those references when the actor is Destroyed."), *DebugName);

In fact this object is the “SObjectWidget” is the “CurrentSlateWidget” Created inside the “void UWidgetComponent::UpdateWidget()” function.

Looks to me that WidgetObject is being removed by GC twice for some reason.

I got stuck with this already for few days. Would be bo great to have some help on this one.

Thank you so much advance.

I have the same problem, but I solved it.
This is resolved by resetting the widget component’s reference to nullptr when the Actor’s EndPlay event fires.

void ACSScreenActor::EndPlay(const EEndPlayReason::Type EndPlayReason)
    WebBrowserWidgetComponent ->SetWidget(nullptr);

But it’s so weird and I don’t know why.
I’m thinking it could be caused by a delegate binding when loading the URL, but I’m not sure.