<h5 dir="auto">System information (version)</h5>

<ul dir="auto">
<li>OpenCV =&gt; 3.4.1</li>
<li>Operating System / Platform =&gt; Windows 64 Bit</li>
<li>Compiler =&gt; Visual Studio 2017</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<p dir="auto"><code class="notranslate">cv::MotionJpegCapture</code> crashes if <code class="notranslate">grabFrame</code> is called after the end of stream is reached due to unchecked iterator increment <code class="notranslate">++m_frame_iterator</code>.</p>
<h5 dir="auto">Steps to reproduce</h5>

<div class="highlight highlight-source-c++ notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#include &lt;opencv2/videoio.hpp&gt;

int main()
{
    cv::VideoCapture capture( &quot;some MJPEG video file&quot; );
    cv::Mat image;
    while( capture &gt;&gt; image, !image.empty() );
    capture &gt;&gt; image; // &lt; deque iterator is not incrementable
    return EXIT_SUCCESS;
}"><pre class="notranslate">#<span class="pl-k">include</span> <span class="pl-s"><span class="pl-pds">&lt;</span>opencv2/videoio.hpp<span class="pl-pds">&gt;</span></span>

<span class="pl-k">int</span> <span class="pl-en">main</span>()
{
    cv::VideoCapture <span class="pl-smi">capture</span>( <span class="pl-s"><span class="pl-pds">"</span>some MJPEG video file<span class="pl-pds">"</span></span> );
    cv::Mat image;
    <span class="pl-k">while</span>( capture &gt;&gt; image, !image.<span class="pl-c1">empty</span>() );
    capture &gt;&gt; image; <span class="pl-c"><span class="pl-c">//</span> &lt; deque iterator is not incrementable</span>
    <span class="pl-k">return</span> EXIT_SUCCESS;
}</pre></div>
<h5 dir="auto">System information (version)</h5>

<ul dir="auto">
<li>OpenCV =&gt; 2.4+ (All versions since 2012)</li>
<li>Operating System / Platform =&gt; Debian 64 Bit</li>
<li>Compiler =&gt; GCC 6</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<h6 dir="auto">Background</h6>
<p dir="auto">I found this issue while trying to use the Conan package manager. I realise that the OpenCV does not provide any official package manager support, but I believe this fix could help others too. <a href="https://github.com/conan-community/community/issues/114">https://github.com/conan-community/community/issues/114</a></p>
<h6 dir="auto">Description</h6>
<p dir="auto">In the FindOpenEXR.cmake file (<a href="https://github.com/opencv/opencv/blob/master/cmake/OpenCVFindOpenEXR.cmake">https://github.com/opencv/opencv/blob/master/cmake/OpenCVFindOpenEXR.cmake</a>), there is support for using a variable called 'OPENEXR_ROOT' to find a version of OpenEXR installed in a user-defined location. Currently this is a cache variable with a default of C:\Deploy and is only defined/useable in Windows:</p>
<div class="highlight highlight-source-cmake notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="if(WIN32)
    SET(OPENEXR_ROOT &quot;C:/Deploy&quot; CACHE STRING &quot;Path to the OpenEXR \&quot;Deploy\&quot; folder&quot;)
    if(CMAKE_CL_64)
        SET(OPENEXR_LIBSEARCH_SUFFIXES x64/Release x64 x64/Debug)
    elseif(MSVC)
        SET(OPENEXR_LIBSEARCH_SUFFIXES Win32/Release Win32 Win32/Debug)
    endif()
else()
    set(OPENEXR_ROOT &quot;&quot;)
endif()"><pre class="notranslate"><span class="pl-k">if</span>(<span class="pl-k">WIN32</span>)
    <span class="pl-c1">SET</span>(OPENEXR_ROOT <span class="pl-s">"C:/Deploy"</span> <span class="pl-k">CACHE</span> STRING <span class="pl-s">"Path to the OpenEXR <span class="pl-cce">\"</span>Deploy<span class="pl-cce">\"</span> folder"</span>)
    <span class="pl-k">if</span>(CMAKE_CL_64)
        <span class="pl-c1">SET</span>(OPENEXR_LIBSEARCH_SUFFIXES x64/Release x64 x64/Debug)
    <span class="pl-k">elseif</span>(MSVC)
        <span class="pl-c1">SET</span>(OPENEXR_LIBSEARCH_SUFFIXES Win32/Release Win32 Win32/Debug)
    <span class="pl-k">endif</span>()
<span class="pl-k">else</span>()
    <span class="pl-c1">set</span>(OPENEXR_ROOT <span class="pl-s">""</span>)
<span class="pl-k">endif</span>()</pre></div>
<p dir="auto">It would be great if it could also be a cache variable for other platforms, we can keep the default to "", as it is being set to now.</p>
<p dir="auto">The second thing that would be required is to change the following blob:</p>
<div class="highlight highlight-source-cmake notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="SET(LIBRARY_PATHS
    /usr/lib
    /usr/local/lib
    /sw/lib
    /opt/local/lib
    &quot;${ProgramFiles_ENV_PATH}/OpenEXR/lib/static&quot;
    &quot;${OPENEXR_ROOT}/lib&quot;)"><pre class="notranslate"><span class="pl-c1">SET</span>(LIBRARY_PATHS
    /usr/lib
    /usr/local/lib
    /sw/lib
    /opt/local/lib
    <span class="pl-s">"<span class="pl-smi">${ProgramFiles_ENV_PATH}</span>/OpenEXR/lib/static"</span>
    <span class="pl-s">"<span class="pl-smi">${OPENEXR_ROOT}</span>/lib"</span>)</pre></div>
<p dir="auto">to</p>
<div class="highlight highlight-source-cmake notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="SET(LIBRARY_PATHS
    &quot;${OPENEXR_ROOT}/lib&quot;
    /usr/lib
    /usr/local/lib
    /sw/lib
    /opt/local/lib
    &quot;${ProgramFiles_ENV_PATH}/OpenEXR/lib/static&quot;)"><pre class="notranslate"><span class="pl-c1">SET</span>(LIBRARY_PATHS
    <span class="pl-s">"<span class="pl-smi">${OPENEXR_ROOT}</span>/lib"</span>
    /usr/lib
    /usr/local/lib
    /sw/lib
    /opt/local/lib
    <span class="pl-s">"<span class="pl-smi">${ProgramFiles_ENV_PATH}</span>/OpenEXR/lib/static"</span>)</pre></div>
<p dir="auto">i.e. if we explicitly define OPENEXR_ROOT then we would expect to search that location first before any system installed versions. This is in particular important for use with package managers (which is where I ran into the problem).</p>
<p dir="auto">If there are problems using that OPENEXR_ROOT for whatever reason, maybe it would be OK to use a new variable instead e.g. OPENEXR_USER_ROOT, which could default?</p>