Using naked pointer; Do I have to free memory manually?

Hello…

I am using a naked pointer to structure as I cant use UPROPERTY() pointer.

something like this

FItemStruct* ItemStruct;

What will happen if something it is pointing to get get deleted, Do i have to use Delete to free memory manually?

If you are handling the lifetime of the object (creating and deleting it) then it should be fine if you keep track properly. Otherwise if you are pointing to something that might be deleted by the engine I highly recommend UPROPERTY if it is a pointer based on a UObject controlled by the engine.

Structures cant have UPROPERTY pointers i guess.

But the structure and the pointer to the structure are part of the same class so they get deleted together when delete the class so wont be a big deal.

I want to know just in case, what will happen if the structure I am pointing to got deleted? While the pointer stays?

A struct can have UPROPERTY if you tag it as USTRUCT and have a generated ustruct body macro with it. If you do this and tag your pointer with uproperty the garbage collecter will handle it correctly and any IsValid() checks with that pointer will be ok. Otherwise if you don’t tag any of these things and the structure gets deleted, it will be pointing at what essentially is garbage in memory. If you want it to be safe at that point you will have to keep track of the lifetime of the struct somehow.

USTRUCT()
struct FItemStruct
{
GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, Category = ItemProperties)
	TSubclassOf<class ABaseInventoryItem> ItemClass;

	int32 Fok;

	UPROPERTY(EditAnywhere, Category = ItemProperties)
	TArray<int32> ItemProperties;

	/** Creates Item from the class properties */
	virtual void CreateItem(UWorld* World, const FVector &SpawnLocation = FVector(0, 0, 0), const FRotator &SpawnRotation = FRotator(0, 0, 0)) const
	{
		World->SpawnActor(ItemClass, &SpawnLocation, &SpawnRotation);
	}

	const ABaseInventoryItem* GetDefaultItem() const
	{
		return ItemClass->GetDefaultObject<ABaseInventoryItem>();
	}

	FItemStruct(TSubclassOf<class ABaseInventoryItem> NewItemClass)
	{
		ItemClass = NewItemClass;
	}

	FItemStruct()
	{
		ItemClass = nullptr;
		Fok = 10;
	}
};

I have done it… Here is my structure

Here I am initializing the pointer

USTRUCT(BlueprintType)
struct FStorageUnit
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY()
	FItemStruct* ItemStruct;

	FStorageUnit()
	{
		ItemStruct = nullptr;
	}

	FStorageUnit(FItemStruct &NewItemStruct)
	{
		ItemStruct = &NewItemStruct;
		//ItemStruct = NewItemStruct;
	}
};

Error
1>C:/Users/Srikant/Documents/Unreal Projects/SurvivalGame/Source/SurvivalGame/Inventory/Storage/InventoryStorage.h(15): error : In InventoryStorage: Inappropriate ‘*’ on variable of type ‘FItemStruct’, cannot have an exposed pointer to this type.
1>Error : Failed to generate code for SurvivalGameEditor - error code: OtherCompilationError (2)

I have got to say that is some weird coding design choices :P. For something like this (I do not have a good idea of what you are doing) I would suggest first of all to have a UClass based off of UObject or AActor, and for the data keeping it in a ustruct (not pointer) that is within the class.

I don’t recommend having a struct with only a pointer (or any pointers really to something it does not own and can be deleted without its knowledge). But looking at your code, if you do not want it to be too different I’d suggest two things to change in FStorageUnit. Get rid of the non-default constructor, and have a function to set the pointer. Second, if all this is is a pointer to a class you can do away with FItemStruct* and try(dunno if it will work) to TSubclassOf or better yet, just copy the content to an FItemStruct object instead of pointer.

If you want to know more about ustructs, Rama explains it better than I can link text.

Also, link text. If you really want to point to it make it a UObject.

If you allocate memory manually then you must delete it manually. Only UObjects are garbage collected for you. The rest follows nomral C++ rules. I would suggest you use a shared pointer or similar form of smart pointer instead of a raw pointer.

I used shared pointer. It works well. But when I exit the game, it crashes…

Allocating memory manually means using the “new” keyword?

What if I have a naked pointer and a structure. Both of them are initialized at the same time and present in a single class(Actor).

Will there be any problem if the actor it deleted some time later?

Well its a bit complicated to explain what exactly I am doing…

There is a TArray of struct FItemStruct that contain the item present in the inventory.(actual variables)

struct FStorageUnit

Is a part of a 2d array containing the inventory.(there are 3 inter-layed structure)

2D arrays don’t replicate well in UE4… So the actual data i am storing in a 1D array.

The 2D array has structure pointers pointing to the location in which the data is stored in the 1D array.

1D array data is replicated but is displayed to the player in 2D array format.

Actually I changed how it worked after this. Now instead of storing the pointer to the structure in the 2d array, i now just store the indices of the location in which the 1D array data is stored. And call that 1D array data by reference.

Its all working but sadly even now, I am using a nake pointer to the 1D array where the data is stored.

TArray<FItemStruct>* StorageBasePtr;

The 2d array is a part of a structure which stores this.

But i don’t think there will be a problem if the array and this pointer are created at the same time and part of the same class. Right?

If you have only one instance of this setup going on I don’t think you will have any issues if you code the destructor of these objects appropriately. I would also suggest not using indices of the location unless the array is static in size and you don’t mess around with it during runtime.

Deconstruct as in? I just Destroy() the actor.

The size of both the arrays(1D and the 2D) wont change after the initial initialization.

The same actor class has the pointer and the variable. So, Do i need to write anything extra or just destroying the actor will get the job done?

If its all within the same actor you don’t need to write anything extra. If you are not doing any C/C++ style memory allocation then it is fine. I’m not an expert on the UE4 garbage collecter, but if you tag your struct variable with UPROPERTY it should handle it. Tagging pointers as UPROPERTY just makes sure when the object its pointing to gets deleted the pointer becomes invalid, but since its pointing to something within itself and they both get destroyed same time no worries.