In Gamebryo, you can use smart pointers to achieve semi-automatic reference counting. This is implemented through the NiPointer. This template class overload the operator so that its instance is represented as a standard pointer. However,
The smart pointer automatically increases and reduces the reference count of the object to which it points, so that the application does not directly call the reference counting function of NiRefOjbect. For example, if A function declares A local smart pointer variable and points to object A, the reference Technology of object A will increase by 1 when assigning values. When the function returns, the local smart pointer is destroyed (beyond the scope), and the reference count of object A is reduced by 1.
Normally, each Gameroy class derived from NiRefObject has a convenient smart pointer declaration, which is represented by adding "Ptr" (NiNodePtr, NiTriShapePtr, etc.) to the type name ).
NiNodePtr spParent = <some node>;
NiNodePtr spChild = NiNew NiNode;
// SpChild points to new object. Reference count is 1. spParent-> AttachChild (spChild );
// Reference count of object pointed to by spChild is now 2, since NiNode objects use smart pointers to refer their children.
SpParent-> DetachChild (spChild );
// Reference count of object pointed to by spChild decreases to 1.
SpChild = 0;
// Reference count of object decreases to 0. object is deleted. as an application pointer of an object derived from NiRefObject, when cross-call reduces the reference count of an object's smart pointer, this object should also be a smart pointer. For example, if you detach a NiAVObject sub-object from the parent node, but you do not want to delete the object when it is detach by the parent node, the pointer to the sub-object in your code should also be a smart pointer. Never make a delete call to the smart pointer. This is very important. This is the only behavior difference between pointer objects on the standard stack and smart pointer objects. Call the delete operation on a smart pointer object,
This object will be deleted regardless of the reference count of the pointer object to which the smart pointer points. If you want to release a smart pointer reference to an object (before the smart pointer is destroyed because it exceeds the scope), assign the smart pointer to 0. This ensures that the object is destroyed when the reference count is reduced to 0. Similarly, you should never delete a NiRefObject Object Pointer, which will permanently destroy this object. As discussed in NiRefObject, smart pointers intelligently point to objects dynamically allocated on the stack. Never point a smart pointer to a stack object (local variable ). Never point a smart pointer to a static object.
To distinguish pointers from smart pointers, Gamebryo uses the "pk" prefix for regular pointers and the "sp" prefix for smart pointers.
If a function uses a smart pointer as a parameter, do not pass a common pointer of an object that is not referenced by other smart pointers into the function as a parameter. If this is done, the object pointer will be destroyed. Next is a detailed description of this problem: 1 The regular pointer pkObject points to a dynamically allocated object, and its reference count is 0.
2. Transmit pkObject to a smart pointer parameter. The Compiler implicitly calls the smart pointer constructor to convert pkObject to a temporary smart pointer and increase the reference count of the object to 1.
3. In the scope of the function call, this temporary smart pointer replicates the copy constructor of the smart pointer to a local smart pointer and increases the reference count of the object to 2.
4. When a function call returns, the local smart pointer is destroyed and the reference count of the object is reduced to 1.
5. When the call is complete, the temporary smart pointer object is also destroyed and the reference count of the object is reduced to 0, which causes the object to be released. When processing a reference counting object, smart pointers are not necessary. Smart pointers are a simple tool. NiRefObject includes the IncRefCount function and DecRefCount function. You can use these functions to add and release references directly. Intelligent Systems are a simple package for these calls. Applications can mix smart pointers and manually modify reference counts. The smart pointer system provides two macros, one that always defines the smart pointer type and the other that is used to convert the smart pointer type. The Macro of the type definition is NiSmartPointer (classname ). Example:
NiSmartPointer (NiNode)
The type conversion macro is NiSmartPointerCast (classname, pkObject ). The following are typical examples:
NiAVObjectPtr spChild = <some pointer known to be an NiNode>;
NiNodePtr spNode = NiSmartPointerCast (NiNode, spChild );
Note that the operation is static and not dynamic during type conversion, so we should use NiSmartPointerCast to convert from: http://www.cnblogs.com/lancidie/archive/2011/08/12/2136076.html when we know that the pointer to be converted is of the correct type