The script in which this lies inherits from UObject and is attached to the player controller. This array is in a struct. I tried saving them to an array declared in the class. But then copying to the struct then fails.
I suspect it’s to do with GC but I don’t know how to prevent it? New to C++ as it is!
Any and all help is appreciated!
Edit: I should also add, I’m not storing huge numbers of items in the array. It’s only when I click and drag I sample positions. And clear the list each time. The error occurs when I Empty/Initialize on a new drag, after 60 seconds.
Edit: Came across this but there’s no solution and I can’t access the link provided.
It sounds that you running out of array index space (but im not sure), maybe you should higher the intervals, remove old data in array (for GC your data in active object in array is too important to be removed) or try different container types:
I store at most 30-50 items in the array. It’s only when I empty it that the problem occurs (after 60 seconds). All is fine until then, even if I purposefully store a huge number of items.
Looking at the callstack, my best bet is that the object that holds your TArray does no longer exist. How are you getting access to this object? Is it possible that you are saving a pointer to this object somewhere? Did you make any modifications in UElementalGestureListener::AddPointToShape by any chance? Can you show me more of the code that you modified and/or added?
I assume it’s all the same problem. It just happens to break in a different place if I’m adding to the list, resetting the current list or re-initializing the struct that owns it, once I pass the 60 seconds.
I add the component to the player controller like this
.h
public:
TSubobjectPtr<class UElementalGestureListener> GListener;
.cpp (initializer)
GListener = PCIP.CreateDefaultSubobject<UElementalGestureListener>(this, TEXT("GestureListener"));
/*Then I call my drag start/update/end methods from the player controller. Was using delegates, but come to think of it, they were causing issues after a while! They were losing their reference. Possibly the same problem? So I changed and directly called the methods.*/
void AElementalPlayerController::OnSwipeStarted(const FVector2D& AnchorPosition, float DownTime)
...
GListener->OnDragStart(AnchorPosition);
//OnDragStartDelegate.ExecuteIfBound(AnchorPosition); //Breaks after a duration
Sorry, I guess I would have to see more code for any of this to make sense.
I think what’s happening is that, by the time the event delegate for the swipe event is called, the object that holds your array is already destroyed. Your object was likely removed by the garbage collector that runs every once in a while. UObject instances will be destroyed if no other UObjects hold a reference to them, i.e. the instance is orphaned. The garbage collector will detect this and destroy the object.