Dynamically allocating third-party class

Hi!

I want to dynamically allocate a third-party class instance inside an Actor but using operator new results to some problems. As a result, can you give me some directions and possible ideas on how to instantiate a third-party class inside an actor in Unreal Engine?

What problems did you encounter with using the new operator?

The class was not initialized correctly. For example, if by default it is int a = 0 it was initialized at a random large number.

I assume that the problem is the overridden new operators of UE.

Are you sure it was supposed to be zero initialized? There are cases in C++ where variables are supposed to be uninitialized, and so will contain whatever initial value of whatever was in their allocated memory previously (like a random large number). Can you show the code where you are using the new operator ?

Is the third party library open source?

First of all, thank you for the interest in the topic.

You can see this more detailed post for the problem I encountered. The third-party library is mujoco (you can find the code here). mjCModel structure can be found in this folder in user_model.h file but the problem is encountered when I use AddGeom() where inside the code there is a new mjCGeom and this is the structure that cannot be initialized correctly. I have checked what you are saying and normally in construction the variables have some default values so not initialized variables is not the case.

Engine/Source/Runtime/Core/Public/Modules/Boilerplate/ModuleBoilerplate.h
overrides new/delete operators.

Can’t see any switch to disable it.

You seem to be suggesting that:

mjCGeom* obj = new mjCGeom(model, _def ? _def : def);

isn’t calling the constructor, right?

Try stepping through this code with the debugger on both the working version and non-working version to see what is going on.

Not exactly. The constructor is called but the initialization is not right. Because some parts of mjCGeom is correctly constructed but some other not.

Yes, that’s right but can you think of any reason for the bug? Or any possible solution to try?

Yes, that’s right but can you think of any reason for the bug? Or any possible solution to try?

Yes, try stepping through this code with the debugger on both the working version and non-working version and then compare the behaviour to see what is going on. Specifically locate the point at which their behaviours diverge.