Using libraries with std::shared_ptrs

I have a third party library that I need to include in my Unreal project. This library is full of std::shared_ptr and expects std::shared_ptrs as part of its API. Its not really feasable to swap them all to TSharedPtr.
I will of course use TSharedPtrs wherever possible, but what are my options here?
How can I safely create std::shared_ptrs on the heap without destroying the Unreal memory system/garbage collection etc?

follow for potential solution: I have the same issue (3rd party with heavy usage of std::shared_ptr) and it’s quite messy, lot of issues on FMEMORY_INLINE_FUNCTION_DECORATOR void FMemory::Free(void* Original)

1 Like

I got mine working. But it was caused by the fact that my third party dll was using a different version of the c++ CRT. It was compiled using Visual Studio 2017, while unreal was compiled using VS 2019 compiler. By compiling both my dll and unreal with vs 2019, and hiding my DLL behind a c style interface, it seemed to work.

I found these two articles extremely helpful:

From what I gather, you might be able to use shared pointers without a restrictive interface, but its recommended not to do so. Passing anything beyond simple data structures and pointers past dll boundaries doesn’t appear to be recommended, although perhaps that only applies to cases where they were built using different compilers or different CRT. In any case, you should avoid it wherever possible, along with any templated objects.

By me using different compilers, it appears that causes my dll to try to use the CRT runtime it was built with, different heap than unreal’s heap. Resulting in heap corruption.

I’m no expert, so I don’t have the best details, but I can at least walk you through what worked in my case. I will continue to refine and improve my project in the future, and see if I can do it without using an extra module interface

EDIT: Nope, looks like using a module interface is definitely required in my case. Trying to use my dll without it caused my heap corruption issues again.

Could you find any solution ?