# vtkCellData::AddArray() crash in some situation - v9.2.2

**URL:** https://discourse.vtk.org/t/vtkcelldata-addarray-crash-in-some-situation-v9-2-2/10105
**Category:** Support
**Created:** [December 7, 2022, 4:23pm UTC](https://discourse.vtk.org/t/vtkcelldata-addarray-crash-in-some-situation-v9-2-2/10105 "2022-12-07T16:23:13Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Nicholas\_Yue](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nicholas_yue/32/3751_2.png) [@Nicholas\_Yue](https://discourse.vtk.org/u/Nicholas_Yue)
#### Post date: [December 7, 2022, 4:23pm UTC](https://discourse.vtk.org/t/vtkcelldata-addarray-crash-in-some-situation-v9-2-2/10105/1 "2022-12-07T16:23:13Z")

</div>

I have the following code which calls AddArray() and it segfault.

```auto
	if (euclidean_areas_exist) {
		printf("xx00100\n");
		vtkNew<vtkFloatArray> vtk_euclidean_areas;
		vtk_euclidean_areas->SetName("eu_areas");
		vtk_euclidean_areas->SetNumberOfComponents(1);
		for (FloatContainer::const_iterator iter = euclidean_areas.begin();
				iter != euclidean_areas.end(); ++iter) {
			vtk_euclidean_areas->InsertNextTuple1((*iter));
		}
		printf("xx00101\n");
		assert(source);
		vtkCellData *cd = source->GetCellData();
		assert(cd);
		cd->AddArray(vtk_euclidean_areas);
		printf("xx00102\n");
	}

```

I have build a debug version of VTK 9.2.2 on Ubuntu 18.04 to trace the source of the crash, it appears to be somehow related to the call to Modified()

gdb stack trace

```auto
Program received signal SIGSEGV, Segmentation fault.
0xffffffffffffffff in ?? ()
(gdb) bt
#0 0xffffffffffffffff in ?? ()
#1 0x0000555555cf85a2 in vtkFieldData::AllocateArrays (this=0x555556afa490, num=1) at /data2/nyue/projects/VTK/9.2.2/VTK-9.2.2/Common/DataModel/vtkFieldData.cxx:437
#2 0x0000555555cf897e in vtkFieldData::SetArray (this=0x555556afa490, i=0, data=0x555556afce20) at /data2/nyue/projects/VTK/9.2.2/VTK-9.2.2/Common/DataModel/vtkFieldData.cxx:457
#3 0x0000555555cf951e in vtkFieldData::AddArray (this=0x555556afa490, array=0x555556afce20) at /data2/nyue/projects/VTK/9.2.2/VTK-9.2.2/Common/DataModel/vtkFieldData.cxx:672
#4 0x0000555555637453 in appendAttributes (h5f=@0x7fffffffcc18: 72057594037927936, vtkMeshName="/vtkOBJ", source=0x555556af9ed0)
    at /home/nyue/projects/GeometryTools/GeometryTools_git/src/combine_vtk_hdf_to_vtp_main.cpp:103
#5 0x0000555555637f9c in main (argc=7, argv=0x7fffffffd068) at /home/nyue/projects/GeometryTools/GeometryTools_git/src/combine_vtk_hdf_to_vtp_main.cpp:185
(gdb) quit
A debugging session is active.

	Inferior 1 [process 28829] will be killed.

Quit anyway? (y or n) y

```

Eclipse step through

 ![image](https://discourse.vtk.org/uploads/default/original/2X/a/a97edf7fcf189c118cbe2f92297de8178a529094.png)

---

<div class="post-metadata">

### Author: ![Nicholas\_Yue](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nicholas_yue/32/3751_2.png) [@Nicholas\_Yue](https://discourse.vtk.org/u/Nicholas_Yue)
#### Post date: [December 7, 2022, 5:29pm UTC](https://discourse.vtk.org/t/vtkcelldata-addarray-crash-in-some-situation-v9-2-2/10105/2 "2022-12-07T17:29:19Z")

</div>

Answering my own question and what I learned.

The source (pun not intended) of my crash was how the source pointer was obtained.

Not working (crash)

```auto
vtkPolyData *source;
source = reader->GetOutput();
// pass source as parameter for further usage

```

Instead, I should use the pointer wrapper provided by VTK

Working

```auto
vtkSmartPointer<vtkPolyData> source;
source = reader->GetOutput();
// pass source as parameter for further usage

```
