[vtkusers] vtkPolyData::InsertNextLinkedPoint corrupted heap

NsPx nspx.roronoa at gmail.com
Fri Sep 28 10:21:48 EDT 2012

I'm answering my own post to give more details. Here is a C++ example 
wich reproduces my heap corruption problem.
Thanks by advance.


#include "vtkPolyData.H"
#include "vtkSmartPointer.H"
#include "vtkSphereSource.H"
#include "vtkIdType.H"
#include "vtkIdList.H"

int main(int argc, char *argv[])
     vtkSmartPointer<vtkSphereSource> source = 

     vtkSmartPointer<vtkPolyData> Mesh = 
     Mesh->DeepCopy( source->GetOutput());

     // cell 0 and cell 1 share the same edge composed by the couple of 
points (0, 8)
     // we create a new point in the middle of the edge
     // then instead of creating four new cells we update cell 0 and 
cell 1 and create only two new cells

     vtkIdType cell0pt0, cell0pt1, cell0pt2;
     vtkIdType cell1pt0, cell1pt1, cell1pt2;

     vtkSmartPointer<vtkIdList> ids_list = 
     Mesh->GetCellPoints( 0, ids_list);

     cell0pt0 = ids_list->GetId(1);
     cell0pt1 = ids_list->GetId(2);
     cell0pt2 = ids_list->GetId(0);

     Mesh->GetCellPoints( 1, ids_list);

     cell1pt0 = ids_list->GetId(0);
     cell1pt2 = ids_list->GetId(1);
     cell1pt1 = ids_list->GetId(2);

     double point0[3], point1[3], newPoint[3];

     Mesh->GetPoints()->GetPoint( cell0pt0, point0);
     Mesh->GetPoints()->GetPoint( cell0pt1, point1);

     for(int i=0; i<3; i++)
         newPoint[i] = 0.5*( point0[i]+ point1[i]);

     // for each cell we create two new cell from the middle of the edge

     // newPoint will be used by four cells
     vtkIdType newId = Mesh->InsertNextLinkedPoint(newPoint,4);

     // we update the current cell 0 by replacing cell0pt1 with the new 
     Mesh->ReplaceCellPoint( 0, cell0pt1, newId);
     Mesh->RemoveReferenceToCell( cell0pt1, 0);
     Mesh->AddReferenceToCell( newId, 0);

     // we add a new cell
     vtkIdType tri1[3];
     tri1[0] = cell0pt2;
     tri1[1] = newId;
     tri1[2] = cell0pt1;

     vtkIdType new_cell0 = Mesh->InsertNextLinkedCell(VTK_TRIANGLE,3,tri1);

     // we update the current cell 1 by replacing cell1pt1 with the new 
     Mesh->ReplaceCellPoint( 1, cell1pt1, newId);
     Mesh->RemoveReferenceToCell( cell1pt1, 1);
     Mesh->AddReferenceToCell( newId, 1);

     // we add a new cell
     vtkIdType tri2[3];
     tri2[0] = cell1pt1;
     tri2[1] = newId;
     tri2[2] = cell1pt2;

     // this line throw a heap corruption alert in debug
     vtkIdType new_cell2 = Mesh->InsertNextLinkedCell(VTK_TRIANGLE,3,tri2);

Le 28/09/2012 11:55, NsPx a écrit :
> Hi all,
> Can someone explain me how vtkPolyData::InsertNextLinkedPoint works ?
> I get heap corruption when trying to split triangles by adding new 
> points at this edge.
> Thanks by advance.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120928/fb37073b/attachment.htm>

More information about the vtkusers mailing list