SceneCapture2D - ConstructTexture2D crashes engine
I am currently trying to implement a photo-feature for my game. I would like to capture an image using the USceneCaptureComponent2D component and get it as a UTexture2D to display in the UI. I have looked through the blueprint-office example and searched the answerhub but I can't seem to find out how to correctly use USceneCaptureComponent2D and UTextureRenderTarget2D. Whenever I try to construct the UTexture2D using ConstructTexture2D my entire engine crashes due to illegal memory access.
The capture component is part of my character class which is based upon the FPS-example.
My header declarations:
Taking the photo:
Does anyone know what I did wrong?
Thanks in advance, Todd
asked Oct 27 '15 at 04:23 PM in C++ Programming
The illegal memory access is caused by the texture target is nor valid when OnPhoto is called.
There are three major faults in your code:
The first one is, sceneCapture is a default sub object but it is not exported, this will make the various property modifications that you have made in the constructor are reverted back to the default values in PostLoad.
The second one is, even if you set sceneCapture as export-able, the render target that you use as its TextureTarget is not export-able in USceneCaptureComponent2D :
The last one is the texture 2d variable that you are using to restore the captured result, lastPhoto, is neither an unreal property nor part of a root set, and this will make it illegible to be garbage collected by the engine and then when it does get collected (as soon as the engine decides to do GC) your pointer will point to an invalid area of the memory (a dangling pointer).
There are several solution to this problem, but this is one that I prefer for this kind of feature:
Note: I haven't tried to compile my code snippet, so i don't guarantee that it will work, but this is roughly what I would do to address the problem.
answered Jul 06 '16 at 06:47 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here