# vtkDataWriter crashes with an array that is a vtkMappedDataArray subclass

**URL:** https://discourse.vtk.org/t/vtkdatawriter-crashes-with-an-array-that-is-a-vtkmappeddataarray-subclass/5517
**Category:** Support
**Created:** [April 7, 2021, 10:46pm UTC](https://discourse.vtk.org/t/vtkdatawriter-crashes-with-an-array-that-is-a-vtkmappeddataarray-subclass/5517 "2021-04-07T22:46:52Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![biagas](https://discourse.vtk.org/user_avatar/discourse.vtk.org/biagas/32/7380_2.png) [@biagas](https://discourse.vtk.org/u/biagas)
#### Post date: [April 7, 2021, 10:46pm UTC](https://discourse.vtk.org/t/vtkdatawriter-crashes-with-an-array-that-is-a-vtkmappeddataarray-subclass/5517/1 "2021-04-07T22:46:52Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Yohann\_Bearzi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/yohann_bearzi/32/671_2.png) [@Yohann\_Bearzi](https://discourse.vtk.org/u/Yohann_Bearzi)
#### Post date: [April 8, 2021, 1:58pm UTC](https://discourse.vtk.org/t/vtkdatawriter-crashes-with-an-array-that-is-a-vtkmappeddataarray-subclass/5517/2 "2021-04-08T13:58:03Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![biagas](https://discourse.vtk.org/user_avatar/discourse.vtk.org/biagas/32/7380_2.png) [@biagas](https://discourse.vtk.org/u/biagas)
#### Post date: [April 8, 2021, 3:00pm UTC](https://discourse.vtk.org/t/vtkdatawriter-crashes-with-an-array-that-is-a-vtkmappeddataarray-subclass/5517/3 "2021-04-08T15:00:17Z")

</div>

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