vtk-m2/docs/changelog/StorageBase-StealArray-returns-delete-function.md
Robert Maynard 674fe1fbf2 StealArray now returns the array and free function as a Pair.
This helps reduces bugs when the callers ask to steal arrays
without getting the free function, or ask for the free function
after stealing the array.
2019-05-01 09:42:57 -04:00

787 B

StorageBasic StealArray() now provides delete function to new owner

Memory that is stolen from VTK-m has to be freed correctly. This is required as the memory could have been allocated with new, malloc or even cudaMallocManaged.

Previously it was very easy to transfer ownership of memory out of VTK-m and either fail to capture the free function, or ask for it after the transfer operation which would return a nullptr. Now stealing an array also provides the free function reducing one source of memory leaks.

To properly steal memory from VTK-m you do the following:

  vtkm::cont::ArrayHandle<T> arrayHandle;
  
  ...
  
  auto* stolen = arrayHandle.StealArray();
  T* ptr = stolen.first;
  auto free_function = stolen.second;
  
  ...

  free_function(ptr);