# The vtkPolyData can not be Deleted After some DeleteCell...

**URL:** https://discourse.vtk.org/t/the-vtkpolydata-can-not-be-deleted-after-some-deletecell/2804
**Category:** Support
**Created:** [March 11, 2020, 4:48pm UTC](https://discourse.vtk.org/t/the-vtkpolydata-can-not-be-deleted-after-some-deletecell/2804 "2020-03-11T16:48:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tdchen](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/858c86/32.png) [@tdchen](https://discourse.vtk.org/u/tdchen)
#### Post date: [March 11, 2020, 4:48pm UTC](https://discourse.vtk.org/t/the-vtkpolydata-can-not-be-deleted-after-some-deletecell/2804/1 "2020-03-11T16:48:46Z")

</div>

Hi vtkers  
I have made a function to simplify a triangle mesh:  
void UpdateTriangles(vtkIdType ia, vtkIdType ib, vtkPolyData \*pTriangles);  
The ia and ib are vertex IDs of an edge and pTriangles points to a triangle mesh.  
The ib will contract to ia to make an edge collapse that will make the mash simpler.  
It works. But after it is called some times, the pTriangles-\>Delete() crashed.  
Does anyone has any suggestion?  
Thank you in advance.  
The code is as following:  
void UpdateTriangles(vtkIdType ia, vtkIdType ib, vtkPolyData \*pTriangles)  
{  
unsigned short i, ncells;  
vtkIdType \*tmp, \*cells;

```
pTriangles->GetPointCells(ib, ncells, tmp);
if(ncells == 0)
	return;

cells = new vtkIdType[ncells];
if(cells == NULL)
	return;
memcpy(cells, tmp, ncells*sizeof(vtkIdType));

//delete first and make marks
for(i=0; i<ncells; i++)
{
	if(pTriangles->IsPointUsedByCell(ia, cells[i]))
	{
		pTriangles->RemoveCellReference(cells[i]);
		pTriangles->DeleteCell(cells[i]);
		cells[i] = -1;
		continue;
	}
}
for(i=0; i<ncells; i++)
{
	if(cells[i] != -1)
	{
		pTriangles->RemoveReferenceToCell(ib, cells[i]);
		pTriangles->AddReferenceToCell(ia, cells[i]);
		pTriangles->ReplaceCellPoint(cells[i], ib, ia);
	}
}
delete [] cells;

```

}

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 12, 2020, 10:54am UTC](https://discourse.vtk.org/t/the-vtkpolydata-can-not-be-deleted-after-some-deletecell/2804/2 "2020-03-12T10:54:56Z")

</div>

How did you allocate the object pointed by `pTriangles`? Did you use C++'s `new` operator or VTK’s `vtkObjectBase::New() `?

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 12, 2020, 10:58am UTC](https://discourse.vtk.org/t/the-vtkpolydata-can-not-be-deleted-after-some-deletecell/2804/3 "2020-03-12T10:58:22Z")

</div>

I’ve also noted you’re mixing low level memory management code (e.g. `delete[]` and `memcpy()`) with high level calls (e.g. `DeleteCell()`). This certainly spells disaster. Chose one style and stick to it throughout the code. Don’t mix them.

---

<div class="post-metadata">

### Author: ![tdchen](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/858c86/32.png) [@tdchen](https://discourse.vtk.org/u/tdchen)
#### Post date: [March 13, 2020, 12:00am UTC](https://discourse.vtk.org/t/the-vtkpolydata-can-not-be-deleted-after-some-deletecell/2804/4 "2020-03-13T00:00:59Z")

</div>

Thank you Paulo Carvalho  
I have found the problem:  
AddReferenceToCell() needs rooms for the point, so you have to use ResizeCellList() for it.  
Thank you.
