Why might SpawnActorAbsolute return a (seemingly) corrupted pointer?

I’m calling the templated version of this function passing position, rotator, and parameters where I’m using the CustomPreSpawnInitialization lambda (as well as specifying a template actor) to set some things different than the template actor.

My issue is that inside the lambda it passes the AActor * and it looks nice and valid all the way to the end of that lambda function. When I get finished with the spawn, it has a value - and it looks like its in the range of a valid pointer, but its not valid - in fact it appears to be garbage as if its been garbage collected - but nothing is being destroyed in between. What confuses me is not why its garbage, but why the pointer returned is different than what the lambda saw - I’ve used this approach before with other situations and not seen this value change.

Is there anything odd about the usage pattern of SpawnActor/Absolute where it might in some cases pass back an invalid reference? Or is there some known bug in the 5.3.2 versions where this is an issue? Interestingly if I use the debugger to point to the original pointer that the pre-initialization saw - but after the spawn completes - all that info is still there and unaffected. It jsut seems like its getting confused somewhere and passing me the wrong pointer reference.

Perhaps if you mark it outside of the lambda as a shared pointer (TSharedPtr) it will persist outside of the scope of the lambda function and not be GC’d while at least one instance of it’s references remains. Try passing it in to the lambda as a TSharedPtr reference.

SpawnActor is allocating the space so the best I could do is pass a reference holder for it, circumventing whatever bug I’m seeing. Certainly seems like this must be a bug as any error situation should be returning nullptr (or generating an exception). Definitely I planned to try to capture it just to see what would happen but I do know that Destroy was not called on any actors during that period so I don’t think its getting garbage collected - its just passing back an invalid pointer.

I should also say that this is a behavior difference between running PIE and standalone.

Ah, never mind. I should have turned off optimization earlier to make sure the debugger wasn’t lying to me and of course it was. My issue must be elsewhere. If anyone else thinks they’ve seen an oddity like this just

#pragma optimize(“”, off)

around your code to make sure you see the same behavior when not optimized

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