Custom class is instanced automatically at startup. Why?

Hello guys, i have a custom class that lets me interface with a third party library.
It’s declared this way (i simplifyed tons of stuffs and omitted some too):

PocketSphinxWrapper.h

UCLASS(Config = Game, Abstract, Within = PlayerController)
class UPocketSphinxWrapper : public UObject, public EventGrabber
{
	GENERATED_UCLASS_BODY()
public:
	virtual ~UPocketSphinxWrapper();
	
};

pocketSphinxWrapper.cpp

UPocketSphinxWrapper::UPocketSphinxWrapper(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
	, m_VoiceCapture(NULL)
	, m_IsRecording(false)
{
	FString hmm = FPaths::ConvertRelativePathToFull(FPaths::GameDir()) + TEXT(MODELDIR) + TEXT("/hmm/voxforge_en_0_1_3");
	FString dict = FPaths::ConvertRelativePathToFull(FPaths::GameDir()) + TEXT(MODELDIR) + TEXT("/dict/repair.dic");
	FString keywords = FPaths::ConvertRelativePathToFull(FPaths::GameDir()) + TEXT(MODELDIR) + TEXT("/dict/keywords.txt");

	UE_LOG(LogTemp, Warning, TEXT("hmm location is %s"), *hmm);
	UE_LOG(LogTemp, Warning, TEXT("dict location is %s"), *dict);

[...]
	
	UE_LOG(LogTemp, Warning, TEXT("INIT POCKETSPHINX: r1 = %d - r2 = %d"), r1, r2);
	UE_LOG(LogTemp, Warning, TEXT("Keywords are: %s"), UTF8_TO_TCHAR(ps_get_kws(m_Ps, TESTSESSIONNAME)));
}

The problem arises from the fact that i’m still not using this class (no references, instances or whatsoever), however when i launch the editor i see this class’s constructor log lines come out, so i suppose it gets instaced somewhere.
Since it interfaces with the voice module, and it will be quite hungry of resources, i’m quite concerned.

Does this happen just in the editor, and why?
I mean: will there be any “extra” instaced object that i don’t know about in the shipped game, or this happens just in the editor?

Thanks id advance for your time.

This is due to the fact that any UObject will create at-least one instance. The instance is used to handle default values. Search for DefaultObject and you will find plenty of places that refers to it.

Another point to consider is that you should not add pure virtual functions (those declared in the header with = 0;) because that will make the class abstract and not instance-able and result in conflicts with the DefaultObject behavior of UObjects.

The DefaultObject behavior is only applied to classes that inherit from UObject. I would recommend to put your custom logic in a class that does not inherit from UObject or event a struct if you want to avoid the behavior.

Cheers,
Moss

Got it. I missed this behaviour :slight_smile:
Thanks man.

np :D, keep in mind to accept the answer :smiley:

Do you know if there’s a way to use the ini files for non-UObject classes?

There is, you can just use the GConfig global variable for it. Here comes an example extracted from the Android Advertising module:

FString AdUnitID;
bool found = GConfig->GetString(TEXT("/Script/AndroidRuntimeSettings.AndroidRuntimeSettings"), TEXT("AdMobAdUnitID"), AdUnitID, GEngineIni);

You specify the value you want to read, a key, the variable where to store the extracted value and the ini file where the value should be found.

Cheers,
Moss

Amazing Moss, i seriously love you <3

Thanks and don’t forget to resolve the thread :smiley: