20200916 UE4 使用TSharedPtr的问题

之前遇到一个问题,在某个函数中使用TSharedPtr来管理一个UObject,待跳出函数的时候(定义域结束),报了很奇怪的一段错误。

代码大概是这样的:

1
2
3
4
void AXX::Foo()
{
TSharedPtr<UObject> MyObj = MakeSharable(NewObject<UObject>());
}

退出函数之后报错:

1
Assertion failed: GetFName() == NAME_None

不用猜它为啥会报错了,因为官方直接给了原因:TSharedPtr不适合用在UObject以及其派生类上。用了就会有问题。

Shared Pointers are not compatible with Unreal objects (UObject and its derived classes). The Engine has a separate memory management system (see Object Handling documentation) for UObject management, and the two systems have no overlap with each other.

Buy Me A Coffee / 捐一杯咖啡的钱
分享这篇文章~
0%
//