Closed raax7 closed 3 weeks ago
I actually tried that. Twice. Both attempts have failed because FMemory functions crash when freeing a lot of memory in a short time. I couldn't track down the cause of the problem so I have given up on both attempts.
I removed all of the Array/Set/Map related code and tried just allocating and freeing memory blocks quickly, and it also caused a crash.
The implementations I used are available in my UnrealContainers repository. The current set of containers in the SDK is UnrealContainersNoAlloc.h.
Hey, thank you for such a quick response.
I just went and tested this for myself, and I'm not having any crashes on my UE4.22 game using FMemory::Malloc, FMemory::Realloc and FMemory::Free.
I ran 2 tests, one allocating lots of memory, reallocating it and then immediately freeing it. The second letting the memory stay allocated, and then freeing all allocated memory at once later. Neither of these resulted in crashes.
This first image is while the test was running. You can see on the left performance monitor showing that the memory is actually being allocated. (performance monitor is displaying the Private Bytes count of the process)
This second image is without doing any allocation, reallocation or freeing, just to prove that it was my memory operations causing the spikes.
This third image is the code I ran for these tests.
I can't think of any reason these tests wouldn't show the same issue you had, so any insight would be great!
Ok, there's two more things you could test:
Realloc(nullptr, 0x10)
is Malloc(0x10)
Realloc(Ptr, 0x0)
is Free(Ptr)
Just tested this, and I'm not getting any crashes either. I also tested it on a bunch of different engine versions.
UE4.24
UE4.25
UE5.0.1
I also tested on UE4.20 but my FMemory::Free sig was incorrect, and I couldn't be bothered to update it yet. If you just ignore the freeing part of the first test, it still wasn't crashing.
Here's the code I used, if it wasn't what you wanted please let me know.
Did you try the 2nd option too, using Realloc for everything? Also maybe increase the number of allocations to around 0x40000 on the test where you alloc and then free instantly.
.
Hi, yes sorry I forgot about this. I will check this again when I'm free today.
Hey, this isn't really an issue but more of a feature request.
I think you should add some basic FMemory functions like FMemory::Malloc, FMemory::Realloc and FMemory::Free. This is very useful because you can then add proper destructors to FStrings, TArrays, etc. I believe, if the memory was allocated by the engine (i.e. the temp FString that is passed into AppendString) you are unable to free it normally, and have to use UE's memory management functions.
It shouldn't be too hard to add these, since the code stays pretty much identical across many, many versions of UE. Hence, so do the signatures.
If you think this would be a good addition, then I could make a pull request and try to add it myself. Although, I do think it would be better if you were to add it since, the code would most likely fit in better with the overall project design.