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

 

 


《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:

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

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

 

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< >

 

 

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

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

 

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

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

 

例子:

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

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

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

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

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.


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

::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仍然引指向着实例!!

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

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

 

Warning:

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

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是个毛线??


例子:

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


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

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

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


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

 


 


Using TScopedPointer to track an object