hey I’m really new to c++ but I’m having an issue with a memory leak. I’ve isolated it to one method but can’t figure out what I’m doing wrong. I have a class that renders a widget to a render target every frame. It works alright for a second or 2 and then my machine runs out of memory. I’m don’t know enough to figure out what I’m doing wrong.
SpectatorCamera.H
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "UMG.h"
#include "SlateBasics.h"
#include "SlateCore.h"
#include "SWidget.h"
#include "WidgetRenderer.h"
#include "SpectatorCamera.generated.h"
UCLASS(Blueprintable)
class UFO_RODEO_API ASpectatorCamera : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ASpectatorCamera();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION(Category = "SpectatorCamera", BlueprintCallable)
void RenderWidgetToTargetEnhanced(bool UseGamma, TextureFilter Filter, UUserWidget * WidgetToRender, FVector2D DrawSize, float DeltaTime, UPARAM(ref) UTextureRenderTarget2D * Target);
FWidgetRenderer * renderer;
};
SpectatorCamera.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "SpectatorCamera.h"
// Sets default values
ASpectatorCamera::ASpectatorCamera()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
renderer = new FWidgetRenderer(false);
}
// Called when the game starts or when spawned
void ASpectatorCamera::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ASpectatorCamera::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ASpectatorCamera::RenderWidgetToTargetEnhanced(bool UseGamma, TextureFilter Filter, UUserWidget * WidgetToRender, FVector2D DrawSize, float DeltaTime, UPARAM(ref) UTextureRenderTarget2D * Target)
{
if (!WidgetToRender) return;
if (DrawSize == FVector2D(0, 0)) return;
if (!Target) return;
TSharedRef<SWidget> ref = WidgetToRender->TakeWidget();
renderer->DrawWidget(Target, ref, DrawSize, DeltaTime);
}
I’m calling RenderWidgetToTargetEnhanced once a tick from a blueprint that inherits from this class. Any help is appreciated.