Skip to content

Use RAII instead of new/delete for wrapper temp array

David Gobbi requested to merge dgobbi/vtk:wrapper-raii-temp-array into master

For array and pointer args, the Python wrappers sometimes have to dynamically allocate an array. This patch adds a simple class that uses stack storage for small arrays, heap storage for large arrays, and releases resources upon destruction.

RAII: Resource Acquisition Is Initialization

-------------------
 New code for wrapping a "float *" argument:
-------------------

  int size1 = ap.GetArgSize(1);
  vtkPythonArgs::Array<float> store1(size1);
  float *temp1 = store1.Data();

  /* <<call C++ method>> */

-------------------
 Old code without RAII:
-------------------

  float *temp1 = NULL;
  float small1[4];
  int size1 = 0;

  if (op)
    {
    size1 = ap.GetArgSize(1);
    if (size1 > 0)
      {
      temp1 = small1;
      if (size1 > 4)
        {
        temp1 = new float[size1];
        }
      }
    }

  /* <<call C++ method>> */

  if (temp1 != small1)
    {
    delete [] temp1;
    }

Merge request reports