# Rendering PointHandleRepresentation2D with cursor type cause crash!

**URL:** https://discourse.vtk.org/t/rendering-pointhandlerepresentation2d-with-cursor-type-cause-crash/4538
**Category:** Development
**Created:** [November 1, 2020, 5:56am UTC](https://discourse.vtk.org/t/rendering-pointhandlerepresentation2d-with-cursor-type-cause-crash/4538 "2020-11-01T05:56:57Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![learn\_forever](https://discourse.vtk.org/user_avatar/discourse.vtk.org/learn_forever/32/754_2.png) [@learn\_forever](https://discourse.vtk.org/u/learn_forever)
#### Post date: [November 1, 2020, 5:56am UTC](https://discourse.vtk.org/t/rendering-pointhandlerepresentation2d-with-cursor-type-cause-crash/4538/1 "2020-11-01T05:56:57Z")

</div>

Just run the following codes, you’ll get a nullptr issue. If the PointHandleRepresentation2D doesn’t have an input cursor type, which means , just comment this line code : handleRep-\>SetCursorShape(cursor2D-\>GetOutput()); ,no crash. Can VTK experts shed some lights on this? Thanks!  
int main(int, char \*[])  
{  
// Create the RenderWindow, Renderer and both Actors  
//  
vtkRenderer \*ren1 = vtkRenderer::New();  
vtkRenderWindow \*renWin = vtkRenderWindow::New();  
renWin-\>AddRenderer(ren1);

```
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);

vtkRegularPolygonSource *cursor2D = vtkRegularPolygonSource::New();
cursor2D->SetNumberOfSides(20);
cursor2D->GeneratePolygonOn();
cursor2D->SetRadius(23);
vtkPointHandleRepresentation2D *handleRep = vtkPointHandleRepresentation2D::New();
double *pos = new double[3]{ 80, 80, 40 };
handleRep->SetWorldPosition(pos);
handleRep->ActiveRepresentationOn();
handleRep->PickableOn();
handleRep->SetHandleSize(18);
handleRep->SetCursorShape(cursor2D->GetOutput());

vtkHandleWidget *handleWidget = vtkHandleWidget::New();
handleWidget->SetInteractor(iren);
handleWidget->SetRepresentation(handleRep);

ren1->SetBackground(0.1, 0.2, 0.4);
renWin->SetSize(300, 300);

handleWidget->ProcessEventsOn();
handleWidget->On();
iren->Initialize();
renWin->Render();

//iren->Start();

return EXIT_SUCCESS;

```

}

---

<div class="post-metadata">

### Author: ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)
#### Post date: [November 1, 2020, 10:05pm UTC](https://discourse.vtk.org/t/rendering-pointhandlerepresentation2d-with-cursor-type-cause-crash/4538/2 "2020-11-01T22:05:30Z")

</div>

```auto
  cursor2D->Update();

```

will fix the crash. However there are other problems.

1. VTK has excellent inbuilt reference counting so I think you should first rewrite your code using `vtkNew`. Once you have done this all the references will be handled automatically.
2. You’ll need to figure how use the widgets.

There are plenty of examples e.g. [VTKExamples](https://lorensen.github.io/VTKExamples/site/), (_no longer active_) mainly using `vtkSmartPointer` and a test site [vtk-examples](https://ajpmaclean.github.io/web-test/site/) using modernised versions of the code from the previously referred site, mainly using `vtkNew`. In both these sites, there are examples using Widgets.

To get you started I have rewritten your code: [main.zip](https://discourse.vtk.org/uploads/short-url/h5seIOVXvSqi5ISslCZVPmeD6Y8.zip) (1.4 KB)
