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

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)

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

//delete first and make marks
for(i=0; i<ncells; i++)
	if(pTriangles->IsPointUsedByCell(ia, cells[i]))
		cells[i] = -1;
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;


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

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.

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.