# How to manage UObject instances

**URL:** <https://forums.unrealengine.com/t/how-to-manage-uobject-instances/630901>\
**Category:** C++\
**Tags:** UClass, unreal-engine, UE5-0, UE4, CPP, question\
**Created:** [August 18, 2022, 9:17am UTC](https://forums.unrealengine.com/t/how-to-manage-uobject-instances/630901 "2022-08-18T09:17:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![GSM](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/gsm/32/482168_2.png) [@GSM](https://forums.unrealengine.com/u/GSM)\
**Post date:** [August 18, 2022, 9:17am UTC](https://forums.unrealengine.com/t/how-to-manage-uobject-instances/630901/1 "2022-08-18T09:17:09Z")

</div>

Hi,

i try to understand how to use allocation of Unreal object in Unreal 4.27.2.  
if i create a class derived from UObject (UMyClass), and i put some instances of it in a TMap of an actor, it works.

```auto
UCLASS()
class DEMO_API UMyClass : public UObject
{
	GENERATED_BODY()
public:

	UPROPERTY(BlueprintReadOnly, Category = "My_Info")
	FString TestString;

	UMyClass();
};

```

```auto
UCLASS()
class DEMO_API AMyActor : public AActor
{
	GENERATED_BODY()
private:
	UPROPERTY(EditAnywhere, Category = MapExample)
	TMap<int, UMyClass*> mapTest;
...
}

void AMyActor::BeginPlay()
{
	Super::BeginPlay();

	mapTest.Add(21, NewObject<UMyClass>() );
}

void AMyActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	if (mapTest.Num() > 0)
	{
		for (auto& entity : mapTest)
		{
			FString msg = FString::Printf(TEXT("AMyActor::Tick first: %d"), entity.Key);
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, msg);
		}
	}
}

```

But if i put my UMyClass in a class derived from UObject, it crashes ☹

```auto
UCLASS()
class DEMO_API AMyActor : public AActor
{
	GENERATED_BODY()
private:
	UMyOtherClass* _Test;
...
}

void AMyActor::AMyActor()
{
	_Test = CreateDefaultSubobject<UMyOtherClass>("MyTest");
}

void AMyActor::BeginPlay()
{
	Super::BeginPlay();

	_Test->Init(this);
}

```

```auto
UCLASS()
class DEMO_API UMyOtherClass : public UObject
{
	GENERATED_BODY()

public:

	UPROPERTY(EditAnywhere, Category = MapExample)
	TMap<int, UMyClass*> mapTest;

	bool Init();
};

bool UMyOtherClass::Init()
{
	mapTest.Add(42, NewObject<UMyClass>()); // It crsh !!!!!

	return true;
}

```

I have tried to use my actor has parent in NewObject and it’s the same thing:

```auto
bool UMyOtherClass::Init(AMyActor* parent)
{
	mapTest.Add(42, NewObject<UMyClass>(parent)); // It crsh !!!!!

	return true;
}

```

How can i manage instances of a UObject in another UObject ?

---

<div class="post-metadata">

**Author:** ![MagForceSeven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/magforceseven/32/906563_2.png) [@MagForceSeven](https://forums.unrealengine.com/u/MagForceSeven)\
**Post date:** [August 19, 2022, 12:10am UTC](https://forums.unrealengine.com/t/how-to-manage-uobject-instances/630901/2 "2022-08-19T00:10:56Z")

</div>

It shouldn’t have anything to do with where the variable is. For your \_Test variable you’re missing the UPROPERTY macro. This allows reflection and therefore the garbage collector to know it’s referenced and not get rid of it.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [February 28, 2024, 2:54pm UTC](https://forums.unrealengine.com/t/how-to-manage-uobject-instances/630901/3 "2024-02-28T14:54:00Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
