1. VTK notes - smart pointer in VTK - vtkNew
C++11 starts to use smart pointer to manage resources, and will automatically manage the increase and decrease of reference count. If it is detected that the reference count of an object is reduced to 0, it will automatically release the resources of the object, so as to achieve the purpose of automatic memory management.
There are three ways to create objects in VTK:
1. Create using the static member method New() in vtkObjectBase and release using Delete();
2. Use smart pointer vtkSmartPointer;
3. vtkNew using RAII mode;
In VTK, objects are created on the heap and must be deleted and released, otherwise memory leakage will occur. Because VTK declares the constructors of vtkObjectBase class and its subclasses as protected types, it does not support self construction and creation, so it cannot be automatically created on the function stack;
If you create an object with a smart pointer, you do not need to manually call the Delete() method to reduce the reference count, because the increase and decrease of the reference count are automatically completed by the smart pointer. VTK implements the concept of convenient automatic memory management using reference counting. Unlike other smart pointers, the reference count remains in the VTK object itself, not in the smart pointer class. The advantage is that the reference count can be increased even if the VTK object is passed as a raw pointer.
1.1,vtkNew
Assign and hold VTK objects.
vtkNew is a class template that uses T::New() to allocate and initialize instances of its template parameters during construction. It assumes that it has a reference during its lifetime and calls T - > delete() when destroyed; Auto cast to the original pointer is for convenience, but if vtnowed will go out of range without increasing its reference count, the user of this method should ensure that this pointer is not returned. It can be regarded as a RAII auto_ptr;
vtkNew is used as a substitute for vtkSmartPointer. In a simple environment, vtkNew can be used to replace vtkSmartPointer;
Note: both the assignment constructor and copy constructor of vtkNew are disabled;
vtk's vtk smartpointer ensures that as long as you use this smart pointer to create objects everywhere, there will be no memory leakage.
vtkSmartPointer can be understood as STD:: shared of STL_ PTR but different.
The reference counter variable of vtkSmartPointer is stored in the managed dynamic object, and STD:: shared_ The dynamic objects managed by PTR are not responsible for reference counting, which is std::shared_ptr own responsibility, std::shared_ptr implements reference counting through copy control.
1.2,vtkNew.h
Code location: Common\Core\vtkNew.h
#ifndef vtkNew_h #define vtkNew_h #include "vtkIOStream.h" class vtkObjectBase; template <class T> class vtkNew { /** * Compile time checking that the class is derived from vtkObjectBase. */ void CheckObjectBase(vtkObjectBase*) {} public: /** * Create a new T on construction. */ vtkNew() : Object(T::New()) { this->CheckObjectBase(this->Object); } //@{ /** * Deletes reference to instance of T on destruction. */ ~vtkNew() { T* obj = this->Object; if (obj) { this->Object = nullptr; obj->Delete(); } } //@} /** * Enable pointer-like dereference syntax. Returns a pointer to the contained * object. */ T* operator->() const { return this->Object; } //@{ /** * Get a raw pointer to the contained object. When using this function be * careful that the reference count does not drop to 0 when using the pointer * returned. This will happen when the vtkNew object goes out of * scope for example. */ T* GetPointer() const { return this->Object; } T* Get() const { return this->Object; } operator T* () const { return static_cast<T*>(this->Object); } //@} /** * Dereference the pointer and return a reference to the contained object. * When using this function be careful that the reference count does not * drop to 0 when using the pointer returned. * This will happen when the vtkNew object goes out of scope for example. */ T& operator*() const { return *static_cast<T*>(this->Object); } private: vtkNew(vtkNew<T> const&) = delete; void operator=(vtkNew<T> const&) = delete; T* Object; }; #endif
reference resources: VTK notes - smart pointer in VTK - vtkNew
reference resources: VTK memory management and smart pointer