UE memory management failure

Hi there!
I’m putting together OculusRift, Unreal Engine and OpenCV. To do this, I’m using:

  1. Oculus Rift DK2 with SDK 5 (SDK 6 not stable with my NVidia drivers)
  2. UE 4.7.6 (VR is still not working on 4.8.2)
  3. OpenCV 2.4.1.0 (OpenCV 3 crashes with UE)

At last I got it, but when I merged my OpenCV code into Unreal project there is a function which is not working properly. I post simplified version here:

cv::findContours(contoursImg, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

That function should take a binary image, detect its contours and fill a std::vector< std::vector< cv::Point>> with detected edges. I have to use these contours later in order to detect some geometries. When the method where I’m using to process images ends, it rises a memory error among nexts:

  1. Couldn’t read memory.
  2. Couldn’t write memory.

If I perform other image processing (like canny, for example: cv::Canny(contoursImg, contoursImg, 100, 100 * 2, 3);) on the same image, it works perfectly… I deduced that the problem rises when free the vector of contours, because it rises at the end of the method (just after return statment at Unreal’s void FMallocTBB::Free( void* Ptr ) function). If i try to make my own vector destruction through clear and swap, error rises at the line where I’m trying to free those resources. BTW, I know C++ should take care of memory freeing of that vector when method ends because it’s out of scope…

As a note, the very same algorithm works with no memory leaks outside UE inside it’s own C++ project.
Any idea, tip, solution, clue, comment, etc… will be very appreciated :slight_smile:
Best regards,

I’m running into the exact same problem with Unreal and OpenCV. I have similar code running on both a normal C++ project and an Unreal project, with the Unreal crashing somewhere in FMemory code as you said.

If you happened across a solution within the year, let me know! I’ll do the same (if the information is still relevant).

Yep, I found a solution. The fact is that Unreal doesn’t fit very well with external containers, not matter if they are C++ standard containers. I belive that the reason is because the vector in return is filled inside OpenCV, which lays inside a DLL and Unreal doesn’t know how to free it (the size or the elements it has to free). That’s why its garbage collector is rising issues. The solution I came across was to compile every code client of OpenCV in another DLL in order to avoid Unreal’s garbage collector to try to manage that portion of source code.

NOTE: to return the data from my DLL I used a old C style structure with 3 integer fields, but there is no problem in returning a class. I did it just in case.

Best regards and, if you need more explanation, do not hesitate to come back to me.

Thank you for the quick reply!

I decided to work around this problem by creating an OpenCV application in C++ and use the network to send the information to Unreal. For my needs, this solution works out better for me, so I’ll likely not need to integrate it into Unreal.

I trust that your advice will help others who are struggling with the same issue!