《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

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章