Forward Decleration With Unique Pointers - nullstar/SecondBrain GitHub Wiki

Forward Decleration With Unique Pointers

Forward decleration of types that are then used in unique pointers can often lead to compile errors as the destructor definition for the declared type isn't available to the unique pointer when it is needed by the compiler. For example the following code will often create this problem:

// A.h

struct A
{
	A() = default;
	~A() = default;
};



// B.h

struct A;

struct B
{
	B();
	TUniquePtr<A> AInstance;
};



// B.cpp

#include "B.h"
#include "A.h"

B::B()
{
	AInstance = MakeUnique<A>();
}

In some cases it is enough to explicitly declare the destructor for your B class in the cpp file since the issue can arrise from the destructor being inlined by the compiler. Therefore:

// A.h

struct A
{
	A() = default;
	~A() = default;
};



// B.h

struct A;

struct B
{
	B();
	~B();
	TUniquePtr<A> AInstance;
};



// B.cpp

#include "B.h"
#include "A.h"

B::B()
{
	AInstance = MakeUnique<A>();
}

B::~B()
{
}

Sometimes however this isn't enough to fix the problem. I believe the problem can exist when A and B live across module boundaries, or potentialy Unreal generated headers might interfere with the solution. In these cases you must declare a custom destructor for A that is visible to B. Therefore the solution becomes:

// A.h

struct A
{
	A() = default;
	~A() = default;
};



// B.h

struct A;

struct B
{
	struct ADestructor
	{
		void operator()(A* pA) const noexcept;
	};

	B();
	TUniquePtr<A, ADestructor> AInstance;
};



// B.cpp

#include "B.h"
#include "A.h"

void B::ADestructor::operator()(A* pA) const noexcept
{
	delete pA;
}

B::B()
{
	AInstance = TUniquePtr<A, ADestructor>(new A());	
}

#CPlusPlus #MemoryManagement #UniquePointers

⚠️ **GitHub.com Fallback** ⚠️