Calling UE Function from Static Function Crash

I often experience crashes when I called some UE functions from my own static custom function (declared as BlueprintPure). Most notably GetDesiredSize() and GetViewportSize(). The error log mentioned “Access Violaion 0x0000...

Such thing can be avoided when I called them from inside a custom function in a derived PlayerController class. I got the impression that some UE functions are intended to NOT be called in a static function. Is this true or is this a bug?

The steps to replicate it are simple. On begin play, create a widget and show it in the viewport. Then made some modification to the widget via blueprint (i.e. set size, set position, set text, etc). Include a call to a static function (made in C++) with BlueprintPure attribute. Have them all executed in a single sequence (upon BeginPlay).

Normally it will crash. Rarely it will succeed. Is it a bug?

Can you share full call stack? And the last execution line in your code that causes crash

Here’s the complete log from crash.

link text

AElementalPlayer is a subclass of PlayerController class. FLong is a struct and it represent a 64-bit signed integer. It supports every functionality that a primitive 32-bit signed integer support.

The crash happen inside UE’s internal function. When I debug it, it points to a NULL location in memory. Hence the crash.

This activity happen right after I created a widget (via Blueprint), showed it on view port, and do some manipulation (SetSize, SetPosition, SetText, etc.) to every UI component inside the widget. It crashes when I used GetDesiredSize() function. If I don’t use that node, then it runs perfectly.

I’m wondering if that node is not to be used freely in any circumstances. Is it like that?

That’s the only feasible way to get the width & height of a text/string in pixels, based on a certain font size. Does UE provide another way of getting this?

link text
link text

It’s may be (very may be) that GetDesiredSize is called too early. Possibly shifting to Tick event can help somehow, but I’am not sure.
There is nothing bad about using static functions, they are pretty much the same as non-static, they just aren’t object-targeted or class-targeted.
But I just looked into engine source code and found nothing crashable.
TWeakPtr must not crash on IsValid call even if it’s null, it’s only… Wait a minute.
Are you sure, that TextWidget pointer to UWidget isn’t null when RibbonSize is being called? Try putting null check there (probably in TextSize function) and call again. You can use “check” macro, this will cause program to break once if condition inside is false, but will not cause crash.

I found a cause, but might not be the root cause. In blueprint, I accidentally forgot to pin a proper UText widget in the first parameter when calling RibbonSize. So it’s null. But oddly, this line is still executed (inside TextSize()):

FVector2D result = widget->GetDesiredSize();

Where in fact widget pointer in that line should be null. So a call to GetDesiredSize() should not be possible. But UE still made it possible and keep on executing until GetCachedWidget() inside GetDesiredSize(). Does UE have this advance capability or did I misunderstand UE architecture in handling null pointers?

Another cause is that GetDesiredSize() only give the perfect value upon Tick(). Outside of Tick(), sometime it is zero and sometime it is the default value of the parameter set in the Editor. Changing parameters (size & position & text value) of a UText widget dynamically in Blueprint, will only be reflected perfectly in Tick(). Are there no other ways?

In short, after I pin the proper UText widget in the RibbonSize node and made the manipulation in Tick(), it is resolved. However, I put a flag and made a check to that flag in Tick(), so it will be executed only once.

It’s just too bad that a call to GetDesiredSize() from UText widget does not immediately retrieve the latest value even after the text content has been changed. Perhaps something to be improved in future UE version?