Error: [AssetLog] Test.uasset: Leak Detected! CustomWidget_43 (CustomWidget) still has living Slate widgets, it or the parent CanvasPanel_0 (CanvasPanel) is keeping them in memory. Make sure all Slate resources (TSharedPtr<SWidget>'s) are being released in the UWidget's ReleaseSlateResources(). Also check the USlot's ReleaseSlateResources().
第一次写slate 就遇到这么优雅的 问题,足以体现Slate 的绝世美好。但是为了实现项目的功能,不断的抄写别人的链式代码。这个东西的编写就没有那么妨碍我们敲击键盘了。
分析原因
当出现问题的时候,能把报错的上下文代码,或者引擎的日志文件看一下是最好的,以上便于提供一个解决问题的思路,如果碍于麻烦,直接看Output Log 日志,也能把问题分析清楚。
c++ 代码如下
TSharedRef<SWidget> UCustomWidget::RebuildWidget()
{
CustomWidget = SNew(SCustomWidget); // TSharedPtr<SCustomWidget> CustomWidget;
CustomWidget->CustomWidgetPtr = this;
return CustomWidget.ToSharedRef();
}
UMG 树级结构如下
通过报错信息分析来看,TSharedPtr<SWidget> 共享指针没有被自动释放,改正办法就是使用弱指针
有兴趣的同学自行去查看官方链接 # Reparenting a UserWidget within that widget’s events can break links between UWidgets and their SWidget
修正bug
TSharedRef<SWidget> UCustomWidget::RebuildWidget()
{
TSharedPtr<SCustomWidget> TempCustomWidget = SNew(SCustomWidget);
TempCustomWidget->CustomWidgetPtr = this;
CustomWidget = TempCustomWidget; // TWeakPtr<SCustomWidget> CustomWidget;
return TempCustomWidget.ToSharedRef();
}
|