I have recently begun to have problems in relation to Unreal’s overriding of the new and delete operators, particularly when passing between the DLLs I am attempting to use and my code. I am consistently getting heap corruptions due to the fact that Unreal seems to use a different allocator than the standard C++ allocator.
I have attempted a few things, mainly looking through the Engine’s source code to find instances of the allocators being used, and to see the different allocators available. I have had a particular interest in GMalloc, and FMalloc as a whole, due to their use in the garbage collection that Unreal does. I have attempted to change GMalloc to use different FMallocs, particularly being interested in MallocAnsi, as I would presume that that falls most closely in line with the default C++ allocators.
Overall, I’m not really sure where to go from here. To reiterate:
- I am attempting to pass data between my code and certain key DLLs I am using - however, due the fact that UE4 uses an inherently different allocator than standard C++ (msvc), I am receiving heap corruption errors whenever something of mine is deallocated by the DLL, or I deallocate something provided by the DLL. Obviously, the second of those two is fairly avoidable, but I cannot prevent the DLLs from deallocating at some point - I do not have that much control over them.
- I have tested out a couple of things, mainly hunting down Unreal’s “new” and “delete” operators to see what they do. Even after finding them, I can’t really remove them, as attempting to comment them out simply results in the build failing at the UnrealHeaderTool.
I’m reaching a dead end here. If I can’t successfully solve or circumvent this issue, I will be forced to stop using Unreal Engine for my product, and likely rebuild from the ground up the past few months of work in another engine. I do hope that this has a solution, and thanks in advance for the help!
Have you thought about creating your own DLL to allocate/deallocate using the standard C++ library (and bypass using Unreal to allocate/deallocate)?
No body force you to use UE4 allocation, no body force you to use UObject and UPROPERTY() this is only thing memory managed by UE4, UE4 don’t alter C++ in any way everything is 100% C++ valid, fact that it use extra tools to generate some code don’t change that. So you are free to use standard C++ library, just keep in mind type used i there is not compatible with UE4 APIs and you need to convert them first to make it work with rest of UE4 code.
If your DLL is so sensitive on memory allocation build a wrapper in main class of your module (if you didn’t notice UE4 is modular and by making C++ project you just making extra module to the engine code), by default there macro that generate module class for you, but you can replace it with your own start up sequence for your module and use it as access point for low level APIs. I recommend to check out ho HTTP module is build as example, there many modules that fucntion similar way with singetlon and Get() function you can use elsewhere:
As long as you not inside UObject class tree any memory allocation should be under your full control, when your module is unloaded you should cease connection from DLL and renew it on when you module is loaded again. UE4 provides wrapper code for platform memory allocation functions in here (just use FPlatfrom insted of FGenericPlatfrom):
But again you free to use standard C++ APIs if you want to just you may hit platform compatibility issues if you try to package to different platforms
So, say that I have a UObject that I place into my world that handles all of the network communication with another program.
At some point, in order to send back some relevant data, I must throw together a data packet that contains some key information about a few of my entities.
I create an instance of the packet class, and after getting the necessary data, call a member of that instance. This member dips into the .DLL in which the class is defined, and everything starts to go bad from there.
As near as I can tell, the error stems from the fact that this member function creates an instance of yet another class, which is then, at the very end of the function, added to a std::vector<…> member within my packet instance. The error is produced by this .DLL-created class instance, which must have something intrinsically different about it, attempting to be .push_back(…)'d into my UE4-created class instance.
This sounds and seems like a difference into allocation between the two, but in the end I’m not 100% positive! Any tips?
Thanks for responding, by the way! I appreciate the help!