# Adding Observer within c++ loadable crashes Slicer

**URL:** <https://discourse.slicer.org/t/adding-observer-within-c-loadable-crashes-slicer/25044>\
**Category:** Development\
**Tags:** loadable-module\
**Created:** [September 1, 2022, 4:25pm UTC](https://discourse.slicer.org/t/adding-observer-within-c-loadable-crashes-slicer/25044 "2022-09-01T16:25:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![leo](https://avatars.discourse-cdn.com/v4/letter/l/c2a13f/32.png) [@leo](https://discourse.slicer.org/u/leo)\
**Post date:** [September 1, 2022, 4:25pm UTC](https://discourse.slicer.org/t/adding-observer-within-c-loadable-crashes-slicer/25044/1 "2022-09-01T16:25:01Z")

</div>

Hello all!  
So I was trying to implement this line of code from Python in c++:

```auto
id = crosshairNode.AddObserver(slicer.vtkMRMLCrosshairNode.CursorPositionModifiedEvent, onMouseMoved)

```

and my first guess was this one:

```auto
  vtkNew<vtkMRMLScene> scene;
    vtkMRMLVolumeNode* volumeNode = vtkMRMLVolumeNode :: SafeDownCast( scene->GetNodeByID("MRHead"));
    vtkMRMLVolumeNode* inputVolumeNode;
    vtkMRMLCrosshairNode* crosshairNode;
    vtkSmartPointer<vtkCallbackCommand> callback;
   crosshairNode -> AddObserver( vtkMRMLCrosshairNode:: CursorPositionModifiedEvent, callback)

```

this crashes my application immediately.

I also tried to add an observer this way, which actually did not crash, but I don’t know if the observer was actually listening.

I tried to create an event handler that had the NodeCallback Type:

```auto
vtkNew<vtkMRMLCoreTestingUtilities::vtkMRMLNodeCallback> eventHandler() {
 vtkNew<vtkMRMLCoreTestingUtilities::vtkMRMLNodeCallback> callback;
   
 qDebug() << "... handler";
    
 return callback;
}
      scene->AddObserver(vtkCommand :: AnyEvent, eventHandler());

```

I woul be really glad for any help.
