Repository: opencv
Issue: Viz3d crashes when it is closed
Labels: bug,auto-transferred,priority: normal,affected: 3.4,category: viz

Transferred from http://code.opencv.org/issues/4191

```
|| Romain Brégier on 2015-02-12 09:56
|| Priority: Normal
|| Affected: branch 'master' (3.0-dev)
|| Category: viz
|| Tracker: Bug
|| Difficulty: 
|| PR: 
|| Platform: x64 / Windows
```
## Viz3d crashes when it is closed

```
Hi,

When testing Viz tutorials, the viewer works as expected, but when I quit the viewer, the exit event is triggered at least twice, causing an access violation exception (interactor == NULL for the second call) :

Here is the code called twice : "vizimpl.cpp", line 86 :
=========================================================
void cv::viz::Viz3d::VizImpl::ExitCallback::Execute(vtkObject*, unsigned long event_id, void*)
{
    if (event_id == vtkCommand::ExitEvent)
    {
        //THIS PART OF CODE IS EXECUTED AT LEAST TWICE
        viz->interactor_->TerminateApp();
        viz->interactor_ = 0;
    }
}

I tested the following example code of the tutorial :
http://docs.opencv.org/trunk/doc/tutorials/viz/widget_pose/widget_pose.html#widget-pose

My settings are :
==========
* Windows 7 64bits
* MSVC12
* VTK 6.1 dynamic libraries.
* OpenCV 3.0 beta
commit ae4cb571ab4fdca3c7be57f0de19743f823daf3f
Author: Alexander Alekhin <alexander.alekhin@itseez.com>
Date:   Fri Nov 7 19:22:46 2014 +0300

    Set status to beta
```
## History
##### Anatoly Baksheev on 2015-02-12 10:43

```
Thanks for reporting. Could you provide a code where you invoke Viz?
```
##### Romain Brégier on 2015-02-12 12:18

```
I performed no modifications in the code snippet shown in the following tutorial :
http://docs.opencv.org/trunk/doc/tutorials/viz/widget_pose/widget_pose.html#widget-pose
(see attached file)

Thanks for your support,
Romain
-   File viz.cpp added
```
##### Romain Brégier on 2015-02-12 12:47

```
Here is an even simpler snippet where I :
* create a Viz3D
* create a WCube (I do not call showWidget)
* Call spin()
* assign a variable just in order to put a break point and ensure that up to here, there are no runtime problem
* End

The program crashes at its end, with an "access violation exception" (see screenshot -in French), but caused by a different function than in my previous post :

The destructor of the Widget class 
In widget.cpp
================
cv::viz::Widget::~Widget()
{
    if (impl_)
    {
        delete impl_; 
// Here the debugger throw me an exception, and I can see impl_->prop == 0
        impl_ = 0;
    }
}


-   File viz_basic_3.cpp added
-   File viz-basic_bug.PNG added
```
##### Anatoly Baksheev on 2015-04-21 12:27

```
The widget problem is pretty odd, I think it's a debugger issue and problem is not in Widget class.

I can't this anywas with VTK 5.8/VS2008. It is really strange that the callback is called after spin(), it is expected only after spinOnce(). But might be VTK 6.1 has some behavioral changes, or you use interleaved spin()/spinOnce(). That is not recommended.

Also this PR https://github.com/Itseez/opencv/pull/3947 seems to has workaround of this. Thanks to @tcavallari.
```
##### Maksim Shabunin on 2015-04-27 09:12

```
-   Target version changed from 3.0-beta to 3.0
```