Repository: opencv
Issue: Mac crash in cvWaitKey after setting POS_FRAMES on file
Labels: bug,category: videoio,platform: ios/osx

##### System information (version)
<!-- Example
- OpenCV => 3.1
- Operating System / Platform => Windows 64 Bit
- Compiler => Visual Studio 2015
-->

- OpenCV => 3.2.0
- Operating System / Platform => Mac OS Yosemite/Sierra (verified on both)
- Compiler => Apple LLVM version 7.0.2 (clang-700.1.81)

##### Detailed description

After opening a file, stepping forward, then stepping backward, there is a reproducible crash.  This is a regression from OpenCV 3.0.0.  (I was not able to test 3.1.0.)

The file is in format "ISO Media, Apple QuickTime movie" according to the "file" program.  It was created with:
```
    cv::VideoWriter outputVideo;
    int codec = CV_FOURCC('r', 'a', 'w', ' ');
    outputVideo.open(filename, codec, 30, cv::Size(640, 480));
```
##### Steps to reproduce

Here is a (lightly edited) backtrace from CoreGraphics, showing "current", "creation", and "destruction":

```
<Warning>: Invalid Connection ID 170963
PScurrentwindowbounds: CGSGetWindowBounds returned error (1000)
<Warning>: CGSConnection: serious error: CID 170963 is dead.
<Warning>: Backtrace (at 11562.2):
<Warning>: CGSConnection: current backtrace::  0   CoreGraphics                        0x00007fff8e2d4266 CGSBacktraceCreate + 112
<Warning>: CGSConnection: current backtrace::  1   CoreGraphics                        0x00007fff8e3bddde CGSLogBacktrace + 15
<Warning>: CGSConnection: current backtrace::  2   CoreGraphics                        0x00007fff8e2d46b2 (anonymous namespace)::Registry::zombie_check(unsigned int) + 56
<Warning>: CGSConnection: current backtrace::  3   CoreGraphics                        0x00007fff8e2d461b CGSConnectionRegistryCheckConnectionID + 55
<Warning>: CGSConnection: current backtrace::  4   CoreGraphics                        0x00007fff8e2d4543 CGSConnectionByID + 160
<Warning>: CGSConnection: current backtrace::  5   CoreGraphics                        0x00007fff8e3268b0 CGSDisableUpdateToken + 9
<Warning>: CGSConnection: current backtrace::  6   AppKit                              0x00007fff978592ea -[NSView _displayRectIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:] + 1832
<Warning>: CGSConnection: current backtrace::  7   AppKit                              0x00007fff978111ae -[NSView displayIfNeeded] + 1876
<Warning>: CGSConnection: current backtrace::  8   AppKit                              0x00007fff97810a09 -[NSWindow displayIfNeeded] + 236
<Warning>: CGSConnection: current backtrace::  9   AppKit                              0x00007fff97810672 _handleWindowNeedsDisplayOrLayoutOrUpdateConstraints + 936
<Warning>: CGSConnection: current backtrace::  10  AppKit                              0x00007fff97f46171 __83-[NSWindow _postWindowNeedsDisplayOrLayoutOrUpdateConstraintsUnlessPostingDisabled]_block_invoke1540 + 46
<Warning>: CGSConnection: current backtrace::  11  CoreFoundation                      0x00007fff95734127 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
<Warning>: CGSConnection: current backtrace::  12  CoreFoundation                      0x00007fff95734080 __CFRunLoopDoObservers + 368
<Warning>: CGSConnection: current backtrace::  13  CoreFoundation                      0x00007fff95726188 __CFRunLoopRun + 872
<Warning>: CGSConnection: current backtrace::  14  CoreFoundation                      0x00007fff95725bd8 CFRunLoopRunSpecific + 296
<Warning>: CGSConnection: current backtrace::  15  HIToolbox                           0x00007fff9471c56f RunCurrentEventLoopInMode + 235
<Warning>: CGSConnection: current backtrace::  16  HIToolbox                           0x00007fff9471c1ee ReceiveNextEventCommon + 179
<Warning>: CGSConnection: current backtrace::  17  HIToolbox                           0x00007fff9471c12b _BlockUntilNextEventMatchingListInModeWithFilter + 71
<Warning>: CGSConnection: current backtrace::  18  AppKit                              0x00007fff9780d8ab _DPSNextEvent + 978
<Warning>: CGSConnection: current backtrace::  19  AppKit                              0x00007fff9780ce58 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 346
<Warning>: CGSConnection: current backtrace::  20  libopencv_highgui.3.2.dylib         0x000000010093acce cvWaitKey + 285
<Warning>: CGSConnection: current backtrace::  21  libopencv_highgui.3.2.dylib         0x00000001009386b3 cv::waitKey(int) + 11
...

<Warning>: Backtrace (at 11560.7):
<Warning>: CGSConnection: creation backtrace::  0   CoreGraphics                        0x00007fff8e2d4266 CGSBacktraceCreate + 112
<Warning>: CGSConnection: creation backtrace::  1   CoreGraphics                        0x00007fff8e2d4008 CGSConnectionRegistryAddConnectionID + 398
<Warning>: CGSConnection: creation backtrace::  2   CoreGraphics                        0x00007fff8e2d3402 CGSNewConnection + 1058
<Warning>: CGSConnection: creation backtrace::  3   AppleGVA                            0x00000001050b8ee4 AVF_CreateMediaAcceleratorInterface + 183728
<Warning>: CGSConnection: creation backtrace::  4   VideoToolbox                        0x00007fff8af60972 VTGetGVADecoderAvailability + 2666
<Warning>: CGSConnection: creation backtrace::  5   VideoToolbox                        0x00007fff8aed5b62 VTDecompressionSessionCreateWithOptions + 2181
<Warning>: CGSConnection: creation backtrace::  6   MediaToolbox                        0x00007fff93e2e6ca VMC2CreateWithQueues + 7737
<Warning>: CGSConnection: creation backtrace::  7   MediaToolbox                        0x00007fff93e2c7b3 VMC2CreateWithCallbacks + 1452
<Warning>: CGSConnection: creation backtrace::  8   MediaToolbox                        0x00007fff93e2c1b9 VMC2CreateWithQueues2 + 559
<Warning>: CGSConnection: creation backtrace::  9   MediaToolbox                        0x00007fff93ef486f FigVideoRenderPipelineCreateWithFigImageQueueArray + 1390
<Warning>: CGSConnection: creation backtrace::  10  MediaToolbox                        0x00007fff93ef4ba0 FigVideoRenderPipelineCreateWithVisualContext + 238
<Warning>: CGSConnection: creation backtrace::  11  MediaToolbox                        0x00007fff93fc4a7e FigAssetWriterCreateWithURL + 54804
<Warning>: CGSConnection: creation backtrace::  12  AVFoundation                        0x00007fff92318db4 -[AVAssetReaderTrackOutput _enableTrackExtractionReturningError:] + 1085
<Warning>: CGSConnection: creation backtrace::  13  AVFoundation                        0x00007fff922af8c1 -[AVAssetReader startReading] + 323
<Warning>: CGSConnection: creation backtrace::  14  libopencv_videoio.3.2.dylib         0x000000010235b4b3 CvCaptureFile::setupReadingAt(CMTime) + 789
<Warning>: CGSConnection: creation backtrace::  15  libopencv_videoio.3.2.dylib         0x000000010235c1d1 CvCaptureFile::setProperty(int, double) + 601
<Warning>: CGSConnection: creation backtrace::  16  libopencv_videoio.3.2.dylib         0x00000001023511f3 cv::VideoCapture::set(CV_CAP_PROP_POS_FRAMES, 0) + 31
...

<Warning>: Backtrace (at 11562.1):
<Warning>: CGSConnection: destruction backtrace::  0   CoreGraphics                        0x00007fff8e2d4266 CGSBacktraceCreate + 112
<Warning>: CGSConnection: destruction backtrace::  1   CoreGraphics                        0x00007fff8e3ab5bd CGSConnectionRegistryRemoveConnectionID + 74
<Warning>: CGSConnection: destruction backtrace::  2   CoreGraphics                        0x00007fff8e3ab38b CGSReleaseConnection + 103
<Warning>: CGSConnection: destruction backtrace::  3   AppleGVA                            0x00000001050b7589 AVF_CreateMediaAcceleratorInterface + 177237
<Warning>: CGSConnection: destruction backtrace::  4   AppleGVA                            0x00000001050ba42e AVF_CreateMediaAcceleratorInterface + 189178
<Warning>: CGSConnection: destruction backtrace::  5   VideoToolbox                        0x00007fff8af61d7c VTGetGVADecoderAvailability + 7796
<Warning>: CGSConnection: destruction backtrace::  6   CoreMedia                           0x00007fff8fed7111 FigBaseObjectFinalize + 73
<Warning>: CGSConnection: destruction backtrace::  7   CoreFoundation                      0x00007fff956cce8e CFRelease + 526
<Warning>: CGSConnection: destruction backtrace::  8   VideoToolbox                        0x00007fff8aed4f4b VTDecompressionSessionInvalidate + 94
<Warning>: CGSConnection: destruction backtrace::  9   MediaToolbox                        0x00007fff93e2bc17 VMC2WaitUntilCompletelyStopped + 171
<Warning>: CGSConnection: destruction backtrace::  10  MediaToolbox                        0x00007fff93e2b8ab VMC2Invalidate + 103
<Warning>: CGSConnection: destruction backtrace::  11  MediaToolbox                        0x00007fff93ef507c FigVideoRenderPipelineCreateWithVisualContext + 1482
<Warning>: CGSConnection: destruction backtrace::  12  MediaToolbox                        0x00007fff93fc1bc4 FigAssetWriterCreateWithURL + 42842
<Warning>: CGSConnection: destruction backtrace::  13  MediaToolbox                        0x00007fff93fc30af FigAssetWriterCreateWithURL + 48197
<Warning>: CGSConnection: destruction backtrace::  14  AVFoundation                        0x00007fff922b1e6d -[AVAssetReader cancelReading] + 336
<Warning>: CGSConnection: destruction backtrace::  15  libopencv_videoio.3.2.dylib         0x000000010235b1f0 CvCaptureFile::setupReadingAt(CMTime) + 82
<Warning>: CGSConnection: destruction backtrace::  16  libopencv_videoio.3.2.dylib         0x000000010235c1d1 CvCaptureFile::setProperty(int, double) + 601
<Warning>: CGSConnection: destruction backtrace::  17  libopencv_videoio.3.2.dylib         0x00000001023511f3 cv::VideoCapture::set(CV_CAP_PROP_POS_FRAMES, 2) + 31
...
```