Skip to content

Commit

Permalink
Fix background widget memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
Razdvizh committed Dec 13, 2024
1 parent a70f94d commit 2a7e23e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
7 changes: 4 additions & 3 deletions Source/VisualU/Private/BackgroundVisualImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
#include "Materials/MaterialInstance.h"
#include "Materials/MaterialInterface.h"

void UBackgroundVisualImage::ReleaseSlateResources(bool bReleaseChildren)
UBackgroundVisualImage::UBackgroundVisualImage(const FObjectInitializer& ObjectInitializer)
: UVisualImage(ObjectInitializer)
{
Super::ReleaseSlateResources(bReleaseChildren);
}

UBackgroundVisualImage::UBackgroundVisualImage(const FObjectInitializer& ObjectInitializer) : UVisualImage(ObjectInitializer)
void UBackgroundVisualImage::ReleaseSlateResources(bool bReleaseChildren)
{
Super::ReleaseSlateResources(bReleaseChildren);
}

void UBackgroundVisualImage::StopTransition()
Expand Down
10 changes: 1 addition & 9 deletions Source/VisualU/Private/VisualController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,8 @@ void UVisualController::PostInitProperties()
Renderer = CreateWidget<UVisualRenderer>(OwningPlayerController, UVisualRenderer::StaticClass());
check(Renderer);

/*Rebuild widget immediately to allocate memory for persistent renderer widgets*/
/*Rebuild widget immediately to create renderer widgets*/
Renderer->TakeWidget();

FTSTicker::GetCoreTicker().AddTicker(FTickerDelegate::CreateLambda([this](float)
{
/*Rebuild widget again on next tick to create persistent renderer widgets*/
Renderer->TakeWidget();

return false;
}));

if (bPlaySound && !Head->Info.Sound.IsNull())
{
Expand Down
2 changes: 0 additions & 2 deletions Source/VisualU/Private/VisualImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ void UVisualImage::ReleaseSlateResources(bool bReleaseChildren)
{
UVisualImageBase::ReleaseSlateResources(bReleaseChildren);

Flipbook = nullptr;
CancelAsyncLoad();
VisualImageSlate.Reset();
}

Expand Down
18 changes: 13 additions & 5 deletions Source/VisualU/Private/VisualRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "Components/TextBlock.h"
#include "Components/CanvasPanel.h"
#include "Components/CanvasPanelSlot.h"
#include "Containers/Ticker.h"
#include "PaperFlipbook.h"
#include "VisualSprite.h"
#include "VisualImage.h"
Expand Down Expand Up @@ -54,6 +55,7 @@ void UVisualRenderer::DrawScene(const FScenario* Scene)
Sprite->AssignSpriteInfo(SpriteData.SpriteInfo);

UCanvasPanelSlot* SpriteSlot = Canvas->AddChildToCanvas(Sprite);
check(SpriteSlot);
SpriteSlot->SetZOrder(SpriteData.ZOrder);
SpriteSlot->SetAnchors(SpriteData.Anchors);
SpriteSlot->SetAutoSize(true);
Expand Down Expand Up @@ -127,11 +129,17 @@ TSharedRef<SWidget> UVisualRenderer::RebuildWidget()
WidgetTree->RootWidget = Canvas;

Background = WidgetTree->ConstructWidget<UBackgroundVisualImage>(UBackgroundVisualImage::StaticClass(), TEXT("Background"));
UCanvasPanelSlot* BackgroundSlot = Canvas->AddChildToCanvas(Background);
check(BackgroundSlot);
BackgroundSlot->SetAnchors(FVisualAnchors::FullScreen);
BackgroundSlot->SetOffsets(FVisualMargin::Zero);
BackgroundSlot->SetZOrder(INT_MIN);

FTSTicker::GetCoreTicker().AddTicker(FTickerDelegate::CreateLambda([this](float)
{
UCanvasPanelSlot* BackgroundSlot = Canvas->AddChildToCanvas(Background);
check(BackgroundSlot);
BackgroundSlot->SetAnchors(FVisualAnchors::FullScreen);
BackgroundSlot->SetOffsets(FVisualMargin::Zero);
BackgroundSlot->SetZOrder(INT_MIN);

return false;
}));

return Super::RebuildWidget();
}
Expand Down
4 changes: 2 additions & 2 deletions Source/VisualU/Public/BackgroundVisualImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class UBackgroundVisualImage final : public UVisualImage
GENERATED_BODY()

public:
UBackgroundVisualImage(const FObjectInitializer& ObjectInitializer);

/**
* Releases memory allocated for slate widgets.
*
* @param bReleaseChildren should memory of child widgets be released
*/
virtual void ReleaseSlateResources(bool bReleaseChildren) override;

UBackgroundVisualImage(const FObjectInitializer& ObjectInitializer);

/**
* Stops ongoing transition.
* Has no effect when called during stopped transition.
Expand Down

0 comments on commit 2a7e23e

Please sign in to comment.