《Unreal Engine4 Scripting with C++ CookBook》:Chapter 3: Memory Management and Smart Pointers


该章结合《Effective Mocern C++》一起看, https://blog.csdn.net/qq_35865125/article/details/103752348


C基础回顾

malloc/free

The basic way to allocate memory for your computer program in C (and C++) is by using malloc().  malloc() designates a block of the computer system's memory for your program's use. Once your program is using a segment of memory, no other program can use
or access that segment of memory. An attempt to access a segment of memory not allocated to your program will generate a "segmentation fault", and represents an illegal operation on most systems.
 

Example:

new/delete

The new operator is almost the same as a malloc call, except that it invokes a constructor call on the object created immediately after the memory is allocated. Objects allocated with the operator new should be deallocated with the operator delete (and not free()).
 

The operator new works by allocating space just as malloc() does. If the type used with the operator new is an object type, the constructor is invoked automatically with the use of the keyword new, whereas the constructor is never invoked with the use of malloc().
 



Managed memory – using NewObject< > and ConstructObject< >

::智能指针是以new/delete为基础的上层建筑。

::注意, AActor 也是派生自 UObject类的。

例子:

:: 查GetTransientPackage();    conglomerate:聚合物。

:: https://blog.csdn.net/qq_35865125/article/details/103449770  有提到UE提供的创建类的实例的函数用法。

You may also want to see the documentation for RF_* flags at  https://docs.unrealengine.com/en-US/index.html .



Managed memory – deallocating memory

 

UObjects are reference-counted and garbage-collected when there are no more references to the UObject instance. Memory allocated on a UObject class derivative using ConstructObject<> or NewObject< > can also be deallocated manually (before the reference count drops to 0) by calling the UObject::ConditionalBeginDestroy() member function.


::BeginDestroy(), FinishDestroy() is ????

Be careful not to call UObject::ConditionalBeginDestroy() on any object still being referenced in memory by other objects' pointers.


 



Managed memory – smart pointers (TSharedPtr, TWeakPtr, TAutoPtr) to track an object
 

---介绍C++语言自己的智能指针。

TSharedPtr is a very useful C++ class that will make any custom C++ object reference-counted—with the exception of UObject derivatives, which are already reference-counted.

An alternate class TWeakPtr is also provided for pointing to a reference-counted object with the strange property of being unable to prevent deletion (hence, "weak").  --- 即,假设又一个类A,现在生成一个A的实例,有2个Shared_Ptr指向了该实例,还有一个weak_ptr指向该实例,当这2个shared_ptr都不再引用该实例时,该实例会被delete掉,即使这时那个weak_ptr仍然引指向着实例!!

::自动被设置成NULL吗,还是只是适用于UE??

Warning:

Always remember that you cannot use TSharedRef with UObjects or UObject derivatives—only on your custom C++ classes, or on your
FStructures can you use any of the TSharedPtr, TSharedRef, TWeakPtr classes to wrap up a raw pointer.  --SharedRef是个毛线??


例子:


关于多线程安全:-- 需要继续查!


此节介绍的太简洁了,之前整理的关于c++智能指针的原理和用法:  https://blog.csdn.net/qq_35865125/article/details/88918909



Using TScopedPointer to track an object

-----太简略了,需要继续查。

A scoped pointer is a pointer that is auto-deleted at the end of the block in which it was declared.   Recall that a scope is just a section of code during which a variable is "alive".    A scope will last until the frst closing brace, }, that occurs.



Unreal's garbage collection system and UPROPERTY( )

 

--- UE的惯例

When you have an object (such as TArray< >) as a UPROPERTY() member of UCLASS(), you need to declare that member as UPROPERTY() (even if you won't edit it in blueprints), otherwise TArray will not stay allocated properly。---狗屁不通。

https://zhuanlan.zhihu.com/p/63269254

Forcing garbage collection
 

When memory flls up, and you want to free some of it, garbage collection can be forced. You seldom need to do this, but you can do it in the case of having a very large texture (or set of textures) that are reference-counted that you need to clear.

单独回收一个object的方法:

Simply call ConditionalBeginDestroy() on all UObjects that you want deallocated from memory, or set their reference counts to 0.

回顾:


using the Profler to identify hot spots

发布了341 篇原创文章 · 获赞 87 · 访问量 22万+

猜你喜欢

转载自blog.csdn.net/qq_35865125/article/details/103749784