Libtorch Integration Model::forward crashes

Libtorch Integration Model::forward crashes

hi, i’ve been trying to integrate libtorch into my project…

i’ve got it to compile & link properly using a self compiled libtorch using RelWithDebInfo to be able to debug it, it also seems to load the dlls without an issue.
i can torch::jit::load() properly no issue, but when i use Model::forward(…) it crashes on std::vector::emplace when it has to deallocate the passed in std::vector < c10::IValue > to resize the std::vector it keeps as internal stack. The exception i get when running under a debugger is 0x80000003 (breakpoint) then 0xc0000374 (heap corruption) if i click continue. Is this something you guys ever encountered/had an issue with? i’m building libtorch from source so i could alter the compile flags…

ue version is 5.3.2
libtorch version is 2.1.2.

compiling using msvc build tools 17.7 (as 17.8 currently has an ICE

Do you have any updates on this?

Still the same issue…

I did end up solving it (no problems yet) - i compiled the mimalloc inside Engine/ThirdParty as dlls and made a new plugin with LoadingPhase “EarliestPossible” which links mimalloc, mimalloc-override, mimalloc-redirect *.dll & *.lib files. inside the ::StartupModule() function of that plugin’s module i call mi_version (ref GitHub - microsoft/mimalloc: mimalloc is a compact general purpose allocator with excellent performance.).

The actual problem ended up being unreal allocating c++ objects with mimalloc, passing them into the libtorch dll which then subsequently deallocated those, under the assumption that they’re allocated with the default allocator.

we’re currently only using torch::jit but have observed no crashes, even with the prebuild binaries from the website.

Here’s an example of the Plugin (by me, don’t trust my binaries with production code, build your own) GitHub - HLennart/MimallocOverride