vtkDataWriter crashes with an array that is a vtkMappedDataArray subclass

I am using VTK-8.1.0 and attempting to create subclass of vtkMappedDataArray for use with non-AOS data. I used vtkCPExodusIINodalCoordinatesTemplate as a guide.

My subclass is being used to represent vtkStructuredGrid coordinates.

When I attempt to use vtkDataSetWriter to serialize the vtkStructuredGrid to a string, my process crashes.

The crash is occurring in vtkDataWriter::WriteArray

First problem:

float *s=GetArrayRawPointer(
data, static_cast<vtkFloatArray *>(data)->GetPointer(0), isAOSArray);

Question: When isAOSArray is false, why would you even want to attempt the cast to vtkFloatArray and call GetPointer on it?

It ends up calling vtkAOSDataArrayTemplate::GetPointer(), which attempts to dereference it’s Buffer object which is NULL.

In some weird instances during my testing, the vtkAOSDataArrayTemplate’s Buffer isn’t NULL, but simply a bad pointer, so the call succeeds, but the bad pointer doesn’t matter because it isn’t being used due to isAOSArray being false.

Second problem is in GetArrayRawPointer:

 vtkSOADataArrayTemplate<T>* typedArray = 
     vtkSOADataArrayTemplate<T>::SafeDownCast(array);
 typedArray->ExportToVoidPointer(data);

The SafeDownCast fails for my subclass of vtkMappedDataArray, returning null for typedArray, but typedArray is then dereferenced without a check.

(Note: this problem has been fixed in VTK-9, which added a guard for valid typedArray before dereferencing, so that’s an easy patch I can apply to prevent the crash.)

The problem remains that without a successful SafeDownCast there is no way for the vtkDataWriter to write the data contained in my class.

I tried modifying GetArrayRawPointer to forgo the SafeDownCast, and simply call
array->ExportToVoidPointer(data).

I re-implemented ExportToVoidPointer for my class, so this change works, but not sure if this will have unforseen consequences.

Is there something that I’ve missed that would allow the vtkMappedDataArray subclass to work correctly with vtkDataWriter?

In VTK master, there isn’t a SafeDownCast anymore in GetArrayRawPointer. ExportToVoidPointer is used instead, so this will be fixed in next release. The call to GetPointer inside WriteArray is not used anymore either, so no bad pointer.

So your temporary fix actually looks like what will be done in future releases (roughly). I don’t think your fix broke anything in the big picture.

Thanks for the tip! I was only looking at 9.0.1 for possible resolution, I should have thought to look at master.