![]() You can easily tell an object owner from an observer. Shared References are never null and can always be dereferenced. Thread safety can be traded out for better performance if it isn't needed. The Unreal Smart Pointer Library includes thread-safe code that manages reference counting across multiple threads. Weak Pointers break reference cycles and prevent dangling pointers. Smart Pointers (other than Weak Pointers) automatically delete objects when there are no more shared references. Similarly, you should not make a Unique Pointer to an object that is referenced by a Shared Pointer or Shared Reference. This will not suspend the Unique Pointer's behavior of deleting the object upon its own destruction, even though other Smart Pointers still reference it. Making a Shared Pointer or Shared Reference to an object that a Unique Pointer references is dangerous. When a Unique Pointer is goes out of scope, it will automatically delete the object it references. Any attempts to copy a Unique Pointer will result in a compile error. Since there can only be one Unique Pointer to a given resource, Unique Pointers can transfer ownership, but cannot share it. For this reason, a Weak Pointer can produce a Shared Pointer to the object it references, ensuring programmers safe access to the object on a temporary basis.Ī Unique Pointer solely and explicitly owns the object it references. This property can be very useful, as it breaks reference cycles, but it also means that a Weak Pointer can become null at any time, without warning. Weak Pointers are similar to Shared Pointers, but do not own the object they reference, and therefore do not affect its lifecycle. Use Shared References when you want a guarantee that the referenced object is non-null, or if you want to indicate shared object ownership. Because Shared Pointers don't have that restriction, a Shared Reference can always be converted to a Shared Pointer, and that Shared Pointer is guaranteed to reference a valid object. They differ with regard to null objects Shared References must always reference a non-null object. Any non-null Shared Pointer can produce a Shared Reference to the object it references.Ī Shared Reference acts like a Shared Pointer, in the sense that it owns the object it references. ![]() A Shared Pointer can be empty, meaning it doesn't reference any object. The following table can be used to help determine when it is appropriate to use each type of Smart Pointer:Ī Shared Pointer owns the object it references, indefinitely preventing deletion of that object, and ultimately handling its deletion when no Shared Pointer or Shared Reference (see below) references it. Different Smart Pointers have different limitations and effects on the object. Smart Pointers can affect the lifespan of the object they contain or reference. ![]() These classes cannot be used with the UObject system because Unreal Objects use a separate memory-tracking system that is better-tuned for game code. It also adds Shared References which act like non-nullable Shared Pointers. ![]() This implementation includes the industry standard Shared Pointers, Weak Pointers, and Unique Pointers. The Unreal Smart Pointer Library is a custom implementation of C++11 smart pointers designed to ease the burden of memory allocation and tracking. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |