When trying to use GEngine i get a access exception error how can i fix it

Hi I’ve been trying to get a asset manager up and running, and I’ve been trying to get the asset manager get function setup for it, but when I use it I get Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000340 error.
from what I can tell this is being caused by GEngine->AssetManager, am doing something wrong?
I would appreciate any help you can offer. also some code and the error message will be below

This the guide I’ve been following

code
UnrealAssetManager.h

#include "CoreMinimal.h"
#include "Engine/AssetManager.h"
#include "UnrealAssetManager.generated.h"

/**
 * 
 */
UCLASS()
class UNREALCOC_API UUnrealAssetManager : public UAssetManager
{
	GENERATED_BODY()
	
public:
	//Class Constructor
	UUnrealAssetManager() {};

	//
	virtual void StartInitialLoading() override;

	//AssetTypes
	static const FPrimaryAssetType  DTAssetType;

	//AssetManager getter function
	static UUnrealAssetManager& GetObj();
};

UnrealAssetManager.cpp

#include "UnrealAssetManager.h"

const FPrimaryAssetType UUnrealAssetManager::DTAssetType = "DataTableAssets";

UUnrealAssetManager& UUnrealAssetManager::GetObj() {
	UUnrealAssetManager* This = Cast<UUnrealAssetManager>(GEngine->AssetManager);

	if (This) {
		return *This;
	}
	else {
		return *NewObject<UUnrealAssetManager>();
	}
}


void UUnrealAssetManager::StartInitialLoading()
{
	Super::StartInitialLoading();

}

UnrealGameInstance.h

#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "UnrealAssetManager.h"
#include "UnrealGameInstance.generated.h"

/**
 * 
 */
UCLASS()
class UNREALCOC_API UUnrealGameInstance : public UGameInstance
{
	GENERATED_BODY()

public:
	UUnrealAssetManager& AM = UUnrealAssetManager::GetObj();

       UUnrealGameInstance();
};

error message
Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000340

UE4Editor_UnrealCOC_9619!UUnrealAssetManager::GetObj() [E:\UE4 games\UnrealCOC\Source\UnrealCOC\UnrealAssetManager.cpp:9]
UE4Editor_UnrealCOC_9619!UUnrealGameInstance::UUnrealGameInstance() [E:\UE4 games\UnrealCOC\Source\UnrealCOC\UnrealGameInstance.h:22]

to properly place code in forum surround it with three backticks ( ` )

Are you sure the error is where you think it is? I can’t entirely tell because the source code isn’t entered correctly, but it looks like you’re blowing up at return *This.

Looking at AssetManager from 4.27, it already implements UAssetManager::Get(), so why are you implementing this function?

I’ve not used the AssetManager personally, but it does look like my project has one, and you don’t need to implement the instantiation of it yourself, just set it in BaseEngine.ini under AssetManagerClassName=/Script/(YourPackage).(ClassName)

Hey thanks for your reply and sorry for the improper formatting I think I fix it.
To answer your questions
1.Why not use default Asset manager get function
the reasons I don’t use the default Asset manager get function is because the asset manager I’m trying to use is custom and I don’t think using the default get function will return the one I need
2.Setting up the asset manager
I did set up my custom asset manager in defaultEngine.ini
3.Was the error caused by This
From what I can tell no, the reason I said I think it caused by GEngine is because in the error message provided it say’s line 9 in Getobj(){} this is the line that UUnrealAssetManager
This = Cast(GEngine->AssetManager); is on

Most of what you see in this code is from the Tutorial I linked

line 9 by my count is “return *This;”

You do not need to implement your own GetObj, you just need to set the correct class name in your BaseEngine.ini. UEngine::IntializeObjectReferences() loads it and calls StartInitialLoading() in your UAssetManager subclass.

and thanks for fixing the code formatting, it’s much easier to understand now :slight_smile:

I managed to get it to work by doing this

UUnrealAssetManager& UUnrealAssetManager::GetObj() {
	UAssetManager* Manager = UAssetManager::GetIfValid();

	if (Manager) {
		//For testing purposes
		/*if (GEngine) {
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Working"));
		}*/

		UUnrealAssetManager* This = Cast<UUnrealAssetManager>(Manager);
		return *This;
	}
	else {
		//For testing purposes
		/*if (GEngine) {
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Not working"));
		}*/
		//Should never be called
		return *NewObject<UUnrealAssetManager>();
	}
}

This should fix the issue

Just some information for any one who’s been reading this
1.When setting up a custom asset manager you need to make a custom getter function for it since the default asset manager only returns UAssetManager reference

You don’t, because it returns whatever object is set as the AssetManager, which is always a subclass of UAssetManager.