当前位置:网站首页>Unrealeengine4 - about uobject's giant pit that is automatically GC garbage collected
Unrealeengine4 - about uobject's giant pit that is automatically GC garbage collected
2022-06-30 04:52:00 【Answer-3】
1 What happened
A static inheritance is created in a plug-in UObject The object of
static UMyObject* m_Object = nullptr;
1
Then it is initialized in the blueprint function of the plug-in :
m_Object = NewObject<UMyObject>();
1
After some time, it is found that this object is destructed , Lead to wild pointer , Cause program crash !
This NIMA is a static object pointer !
2 UE4 Yes UObject Automatic destructor mechanism of
UE4 Yes UObject The object is not like C++,UE4 Will be in UObject Automatically reclaim an object when it has no reference .( So smart , Intelligent makes people's scalp numb )
and C++ If the object is dynamically created, it will always exist in the declaration cycle of the program , If you don't actively destruct , Will cause memory leaks .
On the bright side UE4 This mechanism for preventing memory leaks is perfect , But for those who have just come into contact UE4 For the novice , expected sth. to happen UE4 Adopted C++, But the mechanism is really so different , It is easy to cause the problem of wild pointer .
3 keep UObject Objects are not automatically destructed
If in the ordinary C++ There is one in the class UObject* A Variable , After creating this variable , Best use AddToRoot, In this way, it will not be UE4 Automatically GC, Then in the destructor , Use RemoveFromRoot Can automatically make UE4GC, Prevent memory leaks .
UMyObject* m_Object = NewObject<UMyObject>();
m_Object->AddToRoot();
1
2
And then in C++ Class destructor :
m_Object->RemoveFromRoot();
1
If you are inheriting UObject There is one in the class UObject* A Variable , Then you can use UPROPERTY Macro mark this variable , Then this variable will not be UE4 Automatically GC, When the class is destroyed , Variable A Will be set to null, After that UE4 Automatically GC.
————————————————
Copyright notice : This paper is about CSDN Blogger 「HW140701」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/HW140701/article/details/110149812
边栏推荐
- Approaching history, introduction to the London Guard Museum
- pycharm 数据库工具
- JPA复合主键使用
- Pourquoi l'ordinateur n'a - t - il pas de réseau après l'ouverture du Hotspot win10?
- Thread safety and processing caused by multithreading
- 深度学习------不同方法实现Inception-10
- Unreal 4 learning notes - set player birth point
- 力扣977. 有序数组的平方
- Redis实现短信登入功能(二)Redis实现登入功能
- One command to run rancher
猜你喜欢
力扣977. 有序数组的平方
[fpga] implementation of IIC read / write EEPROM
【Paper】2015_ Active fault-tolerant control system design with trajectory re-planning against actuator
UE4 method of embedding web pages
Create a simple battle game with photon pun
Cheap SSL certificate abroad
MySQL查询小工具(一)json格式的字符串字段中,替换json数组中对象的某个属性值
Unity lens making
Royal Albert Hall, a popular landmark in London
Singapore must visit these scenic spots during the Spring Festival
随机推荐
Sectigo certificate
0 basic unity course. Bricklaying
Circle center technology, very anxious?
Spring Festival Tourism Strategy: welcome the new year in Bangkok, Thailand
Unity a* road finding force planning
Arrays class
What to do when the alicloud SSL certificate expires
Encapsulating JDBC tool classes
The most comprehensive summary notes of redis foundation + advanced project in history
Easyrecovery data recovery software recovers my photo and video data two years ago
Unreal 4 learning notes - data storage using blueprints
Universal Studios Singapore: a good place for a one-day parent-child tour in Singapore
Wildcard SSL certificate issuing time
Unity realizes rotation and Revolution
力扣292周赛题解
深度学习------不同方法实现Inception-10
Marvel fan welfare: Singapore Madame Tussauds Wax Museum Marvel 4D experience Museum
一条命令运行rancher
Oracle-数据的基本操作
图的一些表示方式、邻居和度的介绍