UE 4.26 Crash but I don't see any mistake in C++

Hey,

I’m having sporadic crashes in UE-Editor during testing of my inventory system PLUGIN. Some kind of invalid pointers most likely, maybe GC issue, but I can’t track it down, it’s several weeks now :frowning: .

I have a ItemBasic abstract c++ base class to represent an item base properties (Name, Desc, Weight, Value) and all other item types derive from it. They add some extra data on top of it using derived struct and override some virtual methods of ItemBase class, like ItemArmor.

Error:

ItemBasic.gen.cpp:

DEFINE_FUNCTION(UItemBasic::execGetBase)
{
	P_GET_STRUCT_REF(FItemBaseRow,Z_Param_Out_outBase);
	P_FINISH;
	P_NATIVE_BEGIN;
	P_THIS->GetBase(Z_Param_Out_outBase); // CRASH HAPPENS HERE
	P_NATIVE_END;
}

ItemBasic.h

UCLASS(Blueprintable, Abstract)
class INVENTORYANDINTERACTION_API UItemBasic : public UObject
{
	GENERATED_BODY()

        /* OMMITED */

public:
UFUNCTION(BlueprintCallable, Category = Inventory|Item)
virtual void GetBase(FItemBaseRow& outBase) const;
}

ItemBasic.cpp:

void UItemBasic::GetBase(FItemBaseRow& outBase) const
{
	outBase = FItemBaseRow::Empty;  // static const value, should be always initialized
}

ItemArmor.h

UCLASS(Blueprintable)
class INVENTORYANDINTERACTION_API UItemArmor : public UItemBasic
{
	GENERATED_BODY()

        /* OMMITED */

public:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Inventory|Item)
FItemArmorRow Extra;

virtual void GetBase(FItemBaseRow& outBase) const override;
}

ItemArmor.cpp

void UItemArmor::GetBase(FItemBaseRow& outBase) const
{
	outBase = Extra;
}

I’m using structures inheritance in C++ and it works with BPs without issues, I can upcast/downcast in C++ and pass to BPs, FTableRowBase also works as intended.

USTRUCT(BlueprintType)
struct FItemBaseRow : public FTableRowBase
{
    GENERATED_BODY()

public:
    /* PROPERTIES */

    static const FItemBaseRow Empty;
}

USTRUCT(BlueprintType)
struct FItemArmorRow : public FItemBaseRow
{
    GENERATED_BODY()
public:
    /* PROPERTIES */
}

Any experienced C++ DEV to help?

where is this Extra coming from, how do you define it ?
outBase = Extra;

Greetings @Love_Less

I’m sorry to hear that you’re having trouble. I’ve saw this error with other users prior. It’s been a bit. But, If I recall I think some of them were able to resolve it by switching the render engine to DirectX 11. Have you tried this by chance? If not, I’d start there. Also, have you made any changes to your hardware since the time this started? (New RAM, GPU, etc)

Hi, I will try to switch to DirectX 11 as currently I’m running on DirectX 12, no changes to hardware at all.

I have updated C++ definitions to be more complete.

Extra is defined as a UPROPERTY on each item class type (UItemArmor, UItemWeapon, …) and it is a specific USTRUCT that serves as data holder for such an item class (UItemArmor, UItemWeapon, …). Reason is to have easy serialization using USTRUCTS and flexibility of UFUNCTIONS attached on UCLASS (UItemWeapon, …).

InventoryComponent then internally holds TArray<UItemBasic*> (simplified)

Good deal. Let me know how the testing with DirectX 11 goes. Many cases of the ‘Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0xffffffffffffffff’ get resolved by using DirectX 11 or at least see improvements in the crashing. This crash is most often tied to the GPU or the drivers for it. But, it’s not always the case. Trying DirectX 11 will tell us more. I look forward to hearing back. I hope it helps!

Tried DX11 but it still happens from time to time. My GPU is RTX 3080 12GB, seems it’s related to code than hardware.

image

image

After some testing I find out that sometimes the EXCEPTION_ACCESS_VIOLATION error is NULL pointer (0x0000000000000000), uninitialized pointer (0xFFFFFFFFFFFFFFFF) or invalid pointer (0x0000000000000382)

Which means it must be caused by Garbage Collector? GetBase() method is most likely ok, but the object we call this method on becomes invalid after some time…

Found a solution finally! My error was caused by Garbage Collector who destroyed/invalidated my UObjects silently in the background, thus pointers became invalid or set to null/uninitialized after a while.

My TArray<> and TMap<> structures inside InventoryComponent were not decorated with UPROPERTY() so GC knows about them.

Solution was to decorate them properly:

I did not know we need to decorate containers so GC knows about them :open_mouth:

Hope it helps someone else!

1 Like

Oh, I see! This is an excellent find! Thank you for taking the time to post the solution. This will definitely be of help to others! I’m glad you got it fixed!

1 Like

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