Crash with `FMounteaAdvancedInventorySystemEditor`'s `FMAISCommands::Get().PluginAction`
Describe the bug
When trying to launch my editor I'm getting a crash in FMounteaAdvancedInventorySystemEditor's StartupModule on FMAISCommands::Get().PluginAction.
To Reproduce Steps to reproduce the behavior:
- Launch editor.
Expected behavior Editor doesn't crash on launch.
Desktop (please complete the following information):
- OS: Windows
Callstack
TCommands::Get() Commands.h:74
FMounteaAdvancedInventorySystemEditor::StartupModule() MounteaAdvancedInventorySystemEditor.cpp:209
FModuleManager::LoadModuleWithFailureReason(FName, EModuleLoadResult &, ELoadModuleFlags) ModuleManager.cpp:854
FModuleDescriptor::LoadModulesForPhase(Type, const TArray<…> &, TMap<…> &) ModuleDescriptor.cpp:753
FPluginManager::TryLoadModulesForPlugin(const FPlugin &, Type) PluginManager.cpp:2724
FPluginManager::LoadModulesForEnabledPlugins(Type) PluginManager.cpp:2863
FEngineLoop::Init() LaunchEngineLoop.cpp:4881
EditorInit(IEngineLoop &) UnrealEdGlobals.cpp:109
GuardedMain(const wchar_t *) Launch.cpp:165
LaunchWindowsStartup(HINSTANCE__ *, HINSTANCE__ *, char *, int, const wchar_t *) LaunchWindows.cpp:266
WinMain(HINSTANCE__ *, HINSTANCE__ *, char *, int) LaunchWindows.cpp:317
Additional context I'm using the Rider C++ IDE with UnrealLink and such.
I notice there's a FMAISCommands::Register(); before this, so I'm not sure why the crash is happening. Just for a sanity check I added a check(FMAISCommands::IsRegistered()); before the crash site, and that asserts. So for some reason the registration isn't working as expected.
I tried doing a clean rebuild of this plugin, ran into the same issue. I don't have time to debug this further, I've already spent too much time trying to get this plugin working as it is. 🤐
Looking at the register source code I see this code comment:
// Registering the first command will add the NewInstance into the Binding Manager, who holds on to it.
NewInstance->RegisterCommands();
I believe that means a UI_COMMAND(...) results in a registration, and by having it empty, when we leave FMAISCommands::Register() scope the smart pointer reference count decrements and the registration actually gets cleaned up, and in a way fails since the command instance isn't left intact since it had nothing to register. Best guess.
Upon debugging, yuup, can confirm. FMAISCommands is being deleted upon Register finishing since it didn't register any commands and its smart pointer no longer has any references. This plugin is currently not runnable because of this blocking issue.
I created a change for my own purposes to allow your plugin to start without issue. https://github.com/Mountea-Framework/MounteaInventoryEquipment/compare/main...leetNightshade:MounteaInventoryEquipment:fix_fmaiscommands_crash Not sure what you'll actually end up doing, so I didn't create a PR for this.