Hey guys, this is probably me not understanding C++ completely but is my current problem:
In my character I have a UPROPERTY called AItem* EquipedItem variable and when I touch an object I do this:
EquipedItem = item // item is the actor you touch
item->Destory();
So basically when I touch the pickup object, I want to destroy the pickup that’s on the ground.
Anyways, later in the code when I check if EquipedItem is NULL it shows that its NOT null, after 8-10 seconds it jumps to NULL. Is this because the garbage collector takes some time to kick in? If so, how can I SAFELY check if something is null after someone called Destroy() on it?
If you are using it to check, shouldn’t you be making the pointer NULL at the same time you are destroying the object?
Also, I’m not quite sure why you’d make a pointer to an object, then immediately destroy that object?
It might be easier to help if you described a little of the system you are trying to create, and how it’s meant to work.
when working with pointers to managed objects in UE, always use ->IsValidLowLevel() before accessing it, to determine if an object has been garbage collected (or was never created). Your pointer will of course never point to NULL unless you set it to NULL yourself, maybe you should do that, as Kristiamo said
, you are right, I did this while messing around in C++ for learning purposes, trying different things out and trying to understand how it works. It does not make sense in my example indeed but a situation like that COULD happen in a real-world scenario so I wanted to know how to deal with it when the time comes