Validity checks on pointers not working

So basically, I’ve been having trouble with nullptrs in positions where I really shouldn’t be getting errors. This happens in two places; basically I check to see if the pointer is valid, then I reference it. Almost randomly, it will fail and crash the program.

I don’t know if I’m not checking in the right way, or if it’s just something involved with cross references, but it’s really frustrating and it seems almost impossible to track. Any help would be appreciated; attached is an image of one of the two offenders.

The red x can sometimes be misleading on where the problem actually occurs, especially when templates or inline functions are involved. CFound is obviously valid so I assume the crash occurs within GetActorLocation which gives you the location of the root component so I would check if that is valid as well. E.g. before you call the function also check something like IsValid(CFound->GetRootComponent()).

Here’s more info; thanks for the reply! I tried to check for root component on the first crash; it happens intermittently so we’ll see if that solves it.

This is the other one! This also happens seemingly at random, and I’m just generally confused where to start with this one because I’ve never encountered an error like this. Majority of the time the code executes just fine, but then I’ll get a crash at some point down the line of the sim. Sometimes it doesn’t crash at all and sim goes to completion.

Here’s the start of the call stack :

Note how it’s calling the functions on a supposedly nullptr; even after I check to see if its valid. For reference, it does execute code on the supposedly referenced actor “CurrentConstructionTarget”. The foundation completes, and then it looks for the AI task associated with it; which seems to be corrupted or something. Definitely pointing to some weird obscure part of memory.

This is where things get weird, and we reach the end of our call stack :

Now the value of our AAITaskBuild is an AnimSingleNodeInstance? This makes NO sense to me at all haha; however it isn’t always this, sometimes it reads as “NONE” or some other strange value.

It might be worth noting that this code happens when a ConstructionFoundation transitions into a Structure; the workers building it and the AI tasks associated with the structure get notified and then the foundation should be destroyed.

Completely inexplicable values often indicate UB. However, it could also be that the debugger is simply showing you wrong values e.g. due to optimizations. I hope you are running a debug build? I would also recommend to log the values of the variables in question to make absolutely sure that you are getting correct readings.