Download

DYNAMIC MULTICAST DELEGATE Caused a game crash !

Hello Guys,

I declared a dynamic multi cast in a c++ widget class and called it from it’s child BP, i subcribed to this delegate in my PlayerController c++ class, everything works just fine.

GameWidgetMenu.h


DECLARE_DYNAMIC_MULTICAST_DELEGATE(FPlayButtonEventClickSignature);
/**
*
*/
UCLASS()
class XO_API UGameMenuWidget : public UUserWidget
{ GENERATED_BODY()
 
 

 public: UGameMenuWidget(const FObjectInitializer& obj);
UPROPERTY(BlueprintCallable)
FPlayButtonEventClickSignature PlayDelegate;
 
 

 
};

MyHUD.h



#include "GameMenuWidget.h"
UCLASS()
class XO_API AMyHUD : public AHUD
{
GENERATED_BODY()
private: ULevelSequencePlayer* _var_MenuPlayer;
FScriptDelegate _var_PlayButton;
 
 

 public: AMyHUD();
  void MenuHUD(APlayerController* Who);
void PlayHUD();
 
 

 protected: UMyUserWidget* MainUI;
UGameMenuWidget* _var_MenuWidget;
 
 

 };

MyHUD.cpp


void AMyHUD::MenuHUD(APlayerController* Who)
{ if(MainUI->IsInViewport())
  [INDENT=2]MainUI->RemoveFromViewport();[/INDENT]
  _var_MenuPlayer->PlayLooping();
if (!_var_PlayButton.IsBound())
  [INDENT=2]_var_PlayButton.BindUFunction(Who, "Play");[/INDENT]
  if (!_var_MenuWidget->PlayDelegate.Contains(_var_PlayButton))
  [INDENT=2]_var_MenuWidget->PlayDelegate.Add(_var_PlayButton);[/INDENT]
  _var_MenuWidget->AddToViewport();
 
 

 }
void AMyHUD::PlayHUD()
{ if(_var_MenuPlayer->IsPlaying())
  [INDENT=2]_var_MenuPlayer->Stop();[/INDENT]
  if (_var_MenuWidget->IsInViewport())
  [INDENT=2]_var_MenuWidget->RemoveFromViewport();[/INDENT]
  MainUI->AddToViewport();
 
 

 }

MyPlayerController.h


UCLASS()
class XO_API AMyPlayerController : public APlayerController
{ GENERATED_BODY()
public:
  [INDENT=2]UFUNCTION()
void Play();[/INDENT]
 
 

 };


MyPlayerController.cpp



void AMyPlayerController::BeginPlay()
{ Super::BeginPlay();
Menu();
 
 
}

void AMyPlayerController::Menu()
{ Cast<AMyHUD>(GetHUD())->MenuHUD(this);
bEnableMouseOverEvents = false;
bEnableClickEvents = false;
 
 
}

void AMyPlayerController::Play()
{ Cast<AMyHUD>(GetHUD())->PlayHUD();
bEnableMouseOverEvents = true;
bEnableClickEvents = true;
 
 
}
void AMyPlayerController::Play()
{ Cast<AMyHUD>(GetHUD())->PlayHUD();
bEnableMouseOverEvents = true;
bEnableClickEvents = true;
 
 

 }


GameWidgetBP Graph :


Everything Works fine when i click at the PlayButton, But if i start the game and leave it for 5 minutes and then i click the play button then it crashes !!.

Why ?

I don’t think it’s the delegate that causes the crash. It’s garbage collection. If you want to keep your pointers, you have to mark them with UPROPERTY, so that the garbage collector doesn’t delete the object from memory.



UPROPERTY()
ULevelSequencePlayer* _var_MenuPlayer;

UPROPERTY()
UMyUserWidget* MainUI;


Works Like a CHARM !, Thank you !!