Malloc crash


I have a problem.
I want to allocate some work with FMemory::Malloc, but when i call for a return, it crash the editor.

return static_cast<void*>(FMemory::Malloc(nBytes));

I have followed the exemples in but it doesnt help :frowning:
I cannot give the source code, due to copyright from a third party code, but nByte correspond to a sizeof, no real issue with it.
In fact, in standalone, the code works well. I had to adapt it to Unreal engine with “FMemory”, in standalone the malloc works well, with unreal engine, it crash.

So i wonder, what is major differences between FMemory::Malloc, and malloc ?
This is not very well documented, if somebody have some infos…

ps: im french, sorry for my bad english.

FMallocBinned, FMallocAnsi and FMallocTBB are each subclasses of FMalloc, in my system it ends up calling FMallocTBB::Malloc which itself calls scalable_malloc or scalable_aligned_malloc depending upon whether the alignment is specified. Does the Log or CallStack give you any information in regards to the crash? Is it crashing on an assertion failure, i.e. check, checkSlow etc?

First, thanks for your help ! I bang my head with this issue for days now :slight_smile:

Exception Code: 0xC0000005
Exception Information: The thread tried to read from or write to a virtual address for which it does not have the appropriate access.
Heap Information: Not Present

I don’t have any more informations :confused:

So, you say that Fmemory is safe, it use the Fmallocbinned, ansi, tbb, bined… Do it use MallocThreadSafeProxy too ? All this are non static, do i need to use Fmemory namespace for the Malloc ? Or do i need to use another one ?
In fact, if the data i place with Fmemory::Malloc have too many data, do it handle the memory overflow safely ?
Its pretty hard to understand the UE4 malloc system, there is so few documentations.

Many thanks for your help ! Regards from France :slight_smile:

No you can use FMemory::Malloc, the calls are done by GMalloc->Malloc, GMalloc being a pointer to an instance of FMalloc, its initialisation is done over GCreateMalloc(). An FMallocThreadSafeProxy can be used as well, it’s also derived from FMalloc iirc. Now what you can do is set a breakpoint and step into the appropriate calls, and see what occurs. You can find out what GMalloc is by adding a watch. Lastly if it runs out of memory, it should call FGenericPlatformMemory::OnOutOfMemory(), but that’s if FMallocAnsi is used, as this calls _aligned_malloc/malloc which returns a NULL pointer when it runs out of memory unlike C++'s new which returns an exception (assuming nothrow isn’t used or such). In any case though it should crash if that occurs. :slight_smile:

Well My issue is not related to the malloc, frustated with this issue, ive tryed to comment everything related to malloc, and just make a UE_LOG to a char that must return from my thrird party program, when i do that it crash the same.
when i comment all that library and return a simple char, it crash too…
So i don’t know anymore where the issue come from… I will contact the third party’s owner to figure it out, and will keep searching ^^

Anyway, many many thanks to you reinrag, ive learn a lot with your help, Fmemory seems less dark now :slight_smile:

Great, best of luck. :slight_smile:

Use C++ Operator New Instead of Malloc

Using Malloc does not initialize the Vtable! So virtual functions will crash!

Exception Information:	The thread tried to read from or write to a virtual address for which it does not have the appropriate access.

Also Malloc does not call your constructor!

The only thing malloc does is what it says “memory allocation”

You must properly initialize the virtual functions / call constructor yourself, or do it the clean easy way and use C++ operator new


Use C++ operator **new** to get around this, which both

a. calls the constructor of the data type
b. properly initializes the VTable (virtual function table)

Avoiding Memory Leaks

Please note for every new you must also use c++ operator delete

FYourDataType* NewDataPtr = **new** FYourDataType();

**delete** NewDataPtr;

Always pair them! It is your responsability!

**Wiki Updated**

I updated my wiki with this information

**UE4 Dynamic Memory Management**