Create UObject in StartupModule()

Hi all,

I am attempting to connect to a websocket server in StartupModule() of my plugin. In editor mode, I cannotconnect to a websocket server, but in a packaged game, I can connect. My issue here is, i am creating an UObject that represents a connection to a websocket server, but it is always giving me a rubbish pointer.

Below is an extract of how I create the websocket UObject, where UWebSocket is a derived class of UObject.

StartupModule()
{
    _WebSocket = NewObject<UWebSocket>();
    ...
}

This is the details of the websocket UObject when I debug in VS.

220219-websocket.jpg

Does anyone have any idea why this occurs, or why I can’t create an UObject in StartupModule() of my plugin. I need to have this done in StartupModule() so that in my future projects I do not have to set this up again (or somewhere that will always run my code to connect to a websocket server when my plugin module is loaded).

Hey, what is the loading setting of your moudule ? Maybe that can be related ?
did you try to disable optimisation for that part of code ? ( so you are sure you are looking at the variable after it initialisation and not an optimised oder of execution )

1 Like

Most likely because it gets garbage collected as engine don’t see it referenced anywhere (aka used anywhere) as you not using UPROPERTY() and treats it as trash, as you can’t use those in module class or any other unrefected classes you should use TWeakObjectPtr so you wont get invalid pointer like you have now

Read this how to keep your UObject alive (thru it might be little bit outdated):

I have tried the following, but both didnt really work for editor.

"LoadingPhase": "Default"
"LoadingPhase": "PostEngineInit"

It is definitely not this issue. In my websocket UObject it has uproperties, some of them include delegates. Furthermore, if it was garbage collected, then it wouldn’t work for packaged game either.

It does not matter if your UObject have uproperties. Again, if your UWebSocket object (created by doing this _WebSocket = NewObject<UWebSocket>();) is not referenced by any of monitored variables that have UPROPERTY() it is considered unused/forgotten trash by the engine and deleted by GC.

If you just keeping it in normal pointer (which it seems as module classes are outside of engine reflection system sight) without trying to prevent GC i can guaranty you this is the reason of the issue, your UObject get deleted and you left with invalid pointer, you having typical symptom of it and i don’t belive NewObject gives you dead pointer, if fail it would give you null and print something in log or even crash the engine with assertion check fail.

I had the same adventure with AudioComponent when i was playing with module class for the first time, object just dissapered and i had a crashes that i didn’t understood at beginning as i was not aware of GC function.

If that is the case, do you have any idea for the inconsistency for editor mode and packaged game?

HI, sorry to resurrect this old topic, I did it some time ago!
Even if every object is controlled by GC, there is a way to escape, once you create your object you can add it into the root, like this:

ConfigurationActorsEditor = NewObject<UConfigurationActorsEditor>();
	ConfigurationActorsEditor->AddToRoot();

By doing that you prevent your object to be garbage collected, In this way I manage to have singletons, but at the end all I wanted was access some configurations and for that you can access every UObject with the GetDefault<> or GetMutableDefault<>