x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

[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.

 void AMHUD::BeginPlay()
     {
         Super::BeginPlay();
     
         UVerticalBox* vLayout = NewObject<UVerticalBox>(this, UVerticalBox::StaticClass());
     
         UButton* test = NewObject<UButton>(vLayout, UButton::StaticClass());
     
         UVerticalBoxSlot* buttonSlot = vLayout->AddChildToVerticalBox(test);
     
         buttonSlot->SetHorizontalAlignment(EHorizontalAlignment::HAlign_Center);
         buttonSlot->SetVerticalAlignment(EVerticalAlignment::VAlign_Center);
     
         GEngine->GameViewport->AddViewportWidgetContent(vLayout->TakeWidget());
         
     }

Product Version: UE 4.21
Tags:
more ▼

asked Feb 12 '19 at 08:40 PM in C++ Programming

avatar image

BALKAN BANDIT
12 1 2 5

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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):

 class MYPROJECT_API AMHUD : public AHUD
 {
    GENERATED_BODY()
 
 private:
    UPROPERTY()
    UVerticalBox* vLayout;
      
    UPROPERTY()
    UButton* test;
     
     UPROPERTY() 
     UVerticalBoxSlot* buttonSlot;
 
 // Rest of the code
 };


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")

And this doc: https://docs.unrealengine.com/en-US/Programming/UnrealArchitecture/Objects/Optimizations

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...

more ▼

answered Feb 12 '19 at 08:52 PM

avatar image

LNaej
96 2 8 10

avatar image BALKAN BANDIT Feb 13 '19 at 01:29 PM

Thank you! This actually solved it. And I only need to keep a refrence to my parent widget because it itself will hold refrences to it's children.

avatar image LNaej Feb 13 '19 at 01:35 PM

Awesome, glad I could help!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question