UpdateTextureRegions without Crashing Unreal?

I get a crash when updating my dynamic texture in my Unreal OpenCV project.

The CVTexture object, which doesn’t really wrap anything OpenCV related, is an implementation of a dynamic texture whose texture data can be updated with the contents of a cv::Mat. The UpdateTextureRegions function (which is essentially copied from UTexture2D) crashes inconsistently in some cases. I imagine it’s because of the asynchronous behavior of the call. It might be because there was enough time between it’s executions for the then following operation of the Dynamic Material’s Texture being updated to reference the texture data potentially mid update? I’m not entirely sure what the crash IS but the crash report says that it happens mid way through the operations of the asynchronous function while executing bytecode. I have addressed it so far by switching the order of operations such that the Dynamic Material Texture variable is updated before CVTexture::UpdateTextureRegions is called giving it the time between frames to resolve. This seems to have addressed the problem.

Still I am unhappy that I’ve created nodes that crash Unreal. What can I do to fix this?

Current code can be viewed here.

The solution was to add a tonne of sanity checks before each operation. This routed out a problem possibly relating to a prior session maintaining ownership of the camera device, and this edgecase not being properly handled. Simply put it appears you can open a cv::VideoCapture device without being able to read from it.