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"

UUserWidget pointer is filled with garbage value on runtime, c++

I created UUserWidget and stored it's pointer into TMap. I didn't use AddtoViewport right after the pointer is created at first because I want to use AddtoViewport when I want on runtime. However, the pointer is filled with garbage value when I access it to use AddtoViewport(). Do I have to use AddtoViewport() right after creating UUserWidget? Is there any solution about this situation? Thanks.

    <.h>
     TMap<eWIDGET, UUserWidget*> WidgetMap;
     
     <.cpp>
 void AFP_PlayerController::BeginPlay()
 {
     FName Path = TEXT("WidgetBlueprint'/Game/WidgetBP/FP_StatusWidget_BP.FP_StatusWidget_BP_C'");
         TSubclassOf<UFP_StatusWidget> Widget = Cast<UClass>(StaticLoadObject(UClass::StaticClass(), NULL, *Path.ToString()));
         UFP_StatusWidget* StatusWidget = CreateWidget<UFP_StatusWidget>(this, Widget);
         WidgetMap.Add(STATUS, StatusWidget);
     
         Path = TEXT("WidgetBlueprint'/Game/WidgetBP/FP_Stage_BP.FP_Stage_BP_C'");
         TSubclassOf<UFP_StageWidget> Widget2 = Cast<UClass>(StaticLoadObject(UClass::StaticClass(), NULL, *Path.ToString()));
         UFP_StageWidget* StageWidget = CreateWidget<UFP_StageWidget>(this, Widget2);
         WidgetMap.Add(STAGE, StageWidget);
 }



Product Version: UE 4.17
Tags:
more ▼

asked Nov 21 '17 at 06:37 PM in C++ Programming

avatar image

charelee
7 1 2 3

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

The documentation on garbage collection states that:

"The only container that is safe to have UObject or UObject-derived pointers in is a TArray"

I'd guess that your widget got garbage collected between the time you created it and the time when you tried to call AddtoViewport() for it.

I would suggest replacing the map with an TArray or find some way to store the widget pointer in a class member marked with UPROPERTY to prevent garbage collection.

more ▼

answered Nov 21 '17 at 08:03 PM

avatar image

GoranZ
31 5 2

avatar image charelee Nov 22 '17 at 01:23 AM

You are my savior. Thank you!!!

(comments are locked)
10|2000 characters needed characters left
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