Violation Error on Add to Tarray

Hi,

I am trying to implement an inventory system into my game. I have made an items class which inherits from AActor, a Character class and an inventory class.

The Item is passed as a pointer to a function on Inventory->AddItemToInventory(Item* ItemToInsert); using the Hit.GetActor() method with a static_cast to the item.

All the AddItemToInventory does is call the Add method on the TArray ItemsArray (defined in the header file of Inventory. There is no constructor on the inventory and is created in the characters constructor Inventory* PlayerInventory = new Inventory();

ItemsArray.Add(ItemToInsert);

When I attempt to perform this action I get an Access Violation error:

Exception was "Access violation - code c0000005 (first/second chance not available)"

There are many things that could cause this. Could you post the .h and .cpp files that contain the Inventory and also the thing that uses the Inventory?

sure. (the comment system is being a little bit weird for me atm, not doing anything when I press comment for a long comment so have attached it as a text file. Note the UsableItem constructor just has a simple static mesh.

Inventory Code

The reason your code is crashing is because your Inventory class and instances are not being tracked by the UObject system in UE4. This means that the pointers to your objects in the TArray ItemsArray can become invalid.

You should change your inventory class declaration to be a UStruct instead, and look like this:

USTRUCT(BlueprintType)
struct GRAVITYGAME_API FInventory
    {
    	GENERATED_BODY()

    	void AddItemToInventory(AUsableItem* ItemToInsert);
    	void RemoveItemFromInvetory(AUsableItem* ItemToRemove);

    	// An array which stores the picked up items.
    	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Array)
    	TArray<AUsableItem*> ItemsArray;
    };

(sorry if the formatting was messed up by Answerhub)

You don’t need a constructor. There’s nothing for it to do.

This: Inventory* PlayerInventory;

needs to become this: FInventory PlayerInventory;

You should almost never use the ‘new’ keyword in UE4, except in certain cases such as dealing with the lower-level parts of Slate. There’s no reason for you to create an Inventory object as a raw pointer and create it by hand. It’s not accomplishing anything, it makes things more complicated, and it prevents the UE4 object system from working with it.

By changing it to be a struct (specifically, a UStruct) instead of a pointer to a raw C++ class, it will correctly work as a UPROPERTY, the pointers to UObjects within the inventory array will be tracked correctly by UE4, and you will not need to do manual memory management.

As an example of why you shouldn’t have been doing the manual memory management by hand: your game was leaking every Inventory object you created, because you did not destruct it properly. Also, it would have not worked correctly when creating new instances of your AMainCharacter class from Blueprint subclasses in the editor, since it didn’t deal with copying/CDO correctly.

Also, the UPROPERTY macro you were using in your Inventory class was doing nothing – it only does stuff if you have a GENERATED_BODY() and the class or struct is a UCLASS or a USTRUCT.

Hope that fixes it for you!

Thanks man. Worked like a charm, I’m not sure what I was thinking when I decided I would make it a class cos an inventory doesn’t really represent an object but tracks them instead.