Errant GC causing access issues ("..but is pending kill"), fixable with adding then removing a print

Briefly, I have an Actor-based blueprint class, instantiated by the current level and also stored as a global variable on my GameInstance, with no destroy call, so which should never go out of scope. In my Pawn BeginPlay sequence where I init a UI class, I access this instance via the global and started getting “Attempted to access MyActor … but MyActor is pending kill”. Baffled, I add a retrieve/print just before the UI init call, which accesses MyActor fine, but the UI call still fails. Then I add the same retrieve/print after, and then everything including the UI call works. Then I DELETE both retrieve/prints, leaving everything exactly where I started…and the UI call now continues to work perfectly!

So not only does an actor instantiated in a level seem to randomly disappear, only to be saved by random other accesses, this seems to be path-dependent on the code writing sequence! This smacks of a reference count GC issue, but based on the symptoms I can’t fathom what’s going on; I was really wondering if anyone else has seen this. I think it’s happened a few times but this is the first time I was able to catch and document the non-functioning state.


Details (we have C++, but this example is all in Blueprints):
The Actor is my LevelManager, which contains some variables and functionality that I need on every level, since it’s not easy to make a common level superclass. I drop this Actor into each level, and on BeginPlay each level sets the current global value on GameInstance:
LevelBeginPlay.jpg

Set/GetLevelManager are global blueprint functions that just set/get this as a variable on my GameInstance. This is all logged and has been working fine. Started working on a new 2d UI, created and initialized by my Pawn:
PawnBeginPlay.jpg

SetupUI calls GetLevelManager first thing, which fails with “Warning: Attempted to access LevelManager_2 via property CallFunc_GetLevelManager_LevelManager, but LevelManager_2 is pending kill”.

I then put a Get+Print right before SetupUI, which retrieved LevelManager_2 no problem, but failed with the same error in SetupUI. I put the same Get+Print after AddToViewport, and suddenly everything including the SetupUI access of LevelManager_2 worked perfectly. I then, making no other changes, removed both Get+Prints, and SetupUI continued to work perfectly. LevelManager just holds some parameters and events, and is never itself deleted outside of implicit if its level were to be destroyed (which it isn’t anyway) - nothing shows up with OnDestroyed. FWIW, this is a multi-player app using a dedicated server.

In any case it seems to be working now, but the randomness of it cropping up - and of how I was able to clear it up - is a bit disconcerting, so appreciate if anyone has any insights.

Cheers,
John