TLazySingleton

Lazy singleton that can be torn down explicitly

Windows
MacOS
Linux

Inheritance Hierarchy

FLazySingleton

TLazySingleton

References

Module

Core

Header

/Engine/Source/Runtime/Core/Public/Misc/LazySingleton.h

Include

#include "Misc/LazySingleton.h"

Syntax

template<class T>
class TLazySingleton : public FLazySingleton

Remarks

Lazy singleton that can be torn down explicitly

Defining DISABLE_LAZY_SINGLETON_DESTRUCTION stops automatic static destruction and will instead leak singletons that have not been explicitly torn down. This helps prevent shutdown crashes and deadlocks in quick exit scenarios such as ExitProcess() on Windows.

T must be default constructible.

Example use case:

struct FOo { static FOo& Get(); static void TearDown();

// If default constructor is private friend class FLazySingleton; };

// Only include in .cpp and do not inline Get() and TearDown() #include "Misc/LazySingleton.h"

FOo& FOo::Get() { return TLazySingleton<FOo>::Get(); }

void FOo::TearDown() { TLazySingleton<FOo>::TearDown(); }

Constructors

No constructors are accessible with public or protected access.

Destructors

No destructors are accessible with public or protected access.

Functions

Name Description

Public function Static

T &

 

Get()

Creates singleton once on first call.

Public function Static

void

 

TearDown()

Destroys singleton. No thread must access the singleton during or after this call.

Public function Static

T *

 

TryGet()

Get or create singleton unless it's torn down

Help shape the future of Unreal Engine documentation! Tell us how we're doing so we can serve you better.
Take our survey
Dismiss