[C++] Adding widget to viewport causes crash
Am I doing something wrong in creating these widgets like this, because if I remove this code my game will run just fine.
Also the game will run for about 1 minute before crashing.
I suspect that your UI Objects (vLayout, test etc) are Garbage collected...
You are creating new objects but you are storing the references about them on the stack. In doing so, at the end of your BeginPlay method, these newly created objects are not referenced by anyone anymore. Therefore, they are garbage collected and the ViewPort reference something which is not in memory anymore, leading to a crash.
Try creating class variables with UPROPERTY() referencing them and see if it solves the problem. Something like that (in your header):
Also, attaching the debugger to your game could help track down the issue, thanks to the call stack leading to the crash.
Reading this doc: https://docs.unrealengine.com/en-US/Programming/UnrealArchitecture/Objects/Optimizations (the section "Garbage Collection")
should help you understand this process. :)
Edit: the thing making me not sure about that is the use of the "UWidget::TakeWidget()" method. Don't know what it does about ownership...
Follow this question
Once you sign in you will be able to subscribe for any updates here