Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenCV "Can't fetch data from terminated TLS container" assertion error #35

Closed
dzhoshkun opened this issue Oct 23, 2018 · 1 comment
Closed
Labels

Comments

@dzhoshkun
Copy link
Contributor

The BroadcastDaemon stopping mechanism seems to cause an OpenCV assertion error of the form:

OpenCV(3.4.2) Error: Assertion failed (key_ != -1 && "Can't fetch data from terminated TLS container.") in getData, file /home/dzhoshkun/tools/opencv/modules/core/src/system.cpp, line 1572

With the following patch to c5f590f of the #16 branch, I've managed to narrow it down to the cvtColor function call in the OpenCV video source:

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index a91bcf6..16565ac 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -201,6 +201,8 @@ SET(SOURCES
 # OpenCV components
 if(USE_OPENCV)
     ADD_SUBDIRECTORY(cmake/opencv)
+    LINK_DIRECTORIES("/home/dzhoshkun/ws/_i/opencv/lib")
+    MESSAGE("OpenCV include dirs: ${OpenCV_INCLUDE_DIRS}")
     INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
     LIST(APPEND LIBS ${OpenCV_LIBS})
     ADD_DEFINITIONS(-DUSE_OPENCV)
diff --git a/src/opencv/opencv_video_source.cpp b/src/opencv/opencv_video_source.cpp
index d41ee42..71920c7 100644
--- a/src/opencv/opencv_video_source.cpp
+++ b/src/opencv/opencv_video_source.cpp
@@ -36,6 +36,11 @@ VideoSourceOpenCV::VideoSourceOpenCV(int deviceId)
 
 VideoSourceOpenCV::~VideoSourceOpenCV()
 {
+    {
+    std::lock_guard<std::mutex> buffer_lock_guard(_buffer_lock);
+    _daemon->stop();
+    _cap.release();
+    }
     delete _daemon;
 }
 
@@ -112,20 +117,49 @@ bool VideoSourceOpenCV::get_frame(gg::VideoFrame & frame)
 
     if (!_cap.isOpened()) return false;
 
-    bool has_read = _cap.read(_buffer);
+    bool has_read = false;
+
+    try
+    {
+        has_read = _cap.read(_buffer);
+    }
+    catch (...)
+    {
+        has_read = false;
+        printf("Dzhoshkun in has_read\n");
+        fflush(stdout);
+    }
 
     if (has_read) {
+        try{
+            if (_mybuffer.empty())
+                _mybuffer = cv::Mat::zeros(_buffer.rows, _buffer.cols, CV_8UC3);
+            _buffer.copyTo(_mybuffer);
         // Initialize the BGRA buffer, only if not already done so.
         if (_buffer_bgra.empty() ||
             _buffer_bgra.rows != _buffer.rows ||
             _buffer_bgra.cols != _buffer.cols) {
             _buffer_bgra = cv::Mat::zeros(_buffer.rows, _buffer.cols, CV_8UC4);
         }
+        }
+        catch(...)
+        {
+            printf("Dzhoshkun in #1\n");
+            fflush(stdout);
+        }
+        try{
         // Convert to BGRA
-        cv::cvtColor(_buffer, _buffer_bgra, CV_BGR2BGRA);
+        cv::cvtColor(_mybuffer, _buffer_bgra, CV_BGR2BGRA);
+        }
+        catch(...)
+        {
+            printf("Dzhoshkun in cvtColor\n");
+            fflush(stdout);
+        }
         unsigned char * data = nullptr;
         size_t data_length = 0;
         size_t cols = 0, rows = 0;
+        try{
         if (_get_sub_frame) {
             _buffer_bgra(_sub_frame).copyTo(_buffer_sub_bgra);
             data = _buffer_sub_bgra.data;
@@ -140,6 +174,12 @@ bool VideoSourceOpenCV::get_frame(gg::VideoFrame & frame)
             rows = _buffer_bgra.rows;
             data_length = _buffer_bgra.channels() * cols * rows;
         }
+        }
+        catch(...)
+        {
+            printf("Dzhoshkun in #2\n");
+            fflush(stdout);
+        }
 
         frame.init_from_specs(data, data_length, cols, rows);
     }
diff --git a/src/opencv/opencv_video_source.h b/src/opencv/opencv_video_source.h
index da40aae..8969487 100644
--- a/src/opencv/opencv_video_source.h
+++ b/src/opencv/opencv_video_source.h
@@ -62,6 +62,7 @@ protected:
     gg::BroadcastDaemon * _daemon;
 
 private:
+    cv::Mat _mybuffer;
     //!
     //! \brief Frame rate (fps), needed as a
     //! variable esp. in case it needs to be
diff --git a/src/tests/pipeline/complex_pipeline.py b/src/tests/pipeline/complex_pipeline.py
index 864a2e9..e4332f1 100755
--- a/src/tests/pipeline/complex_pipeline.py
+++ b/src/tests/pipeline/complex_pipeline.py
@@ -231,7 +231,7 @@ if __name__ == '__main__':
     green_dyer.attach(yellow_writer)
     green_dyer.attach(yellow_snapshots)
 
-    sleep(20)  # operate pipeline for 20 sec
+    sleep(5)  # operate pipeline for 20 sec
 
     # stop the histogrammers
     hist_red.stop()
diff --git a/src/tests/pipeline/run-complex-pipeline.sh b/src/tests/pipeline/run-complex-pipeline.sh
index 8a01678..c9fde6f 100755
--- a/src/tests/pipeline/run-complex-pipeline.sh
+++ b/src/tests/pipeline/run-complex-pipeline.sh
@@ -51,6 +51,7 @@ if [ "$1" = "decklink" ]; then
     fi
     CMAKE_OPTS="$CMAKE_OPTS -D USE_BLACKMAGIC_DECKLINK_4K_EXTREME_12G=ON -D ENABLE_NONFREE=ON"
 elif [ "$1" = "dvi2pcie" ]; then
+    CMAKE_OPTS="$CMAKE_OPTS -D OpenCV_DIR=/home/dzhoshkun/ws/_i/opencv/share/OpenCV"  # OpenCV with debug info
     CMAKE_OPTS="$CMAKE_OPTS -D USE_EPIPHAN_DVI2PCIE_DUO=ON -D USE_I420=OFF"
 fi
 CMAKE_OPTS="$CMAKE_OPTS -D CMAKE_BUILD_TYPE=Debug"

This issue is relatively harmless as it always seems to occur at the end of the lifetime of VideoSourceOpenCV objects, i.e. when an application is terminating.
Some more pointers about this issue are available here: UM-ARM-Lab/kinect2_calibration_files#3

This relates to #9. If we go ahead with #9, which is now likely, this issue can be closed.

dzhoshkun added a commit that referenced this issue Oct 23, 2018
@joubs
Copy link

joubs commented Dec 2, 2020

Closing as out of date.

@joubs joubs closed this as completed Dec 2, 2020
@joubs joubs added the stale label Dec 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants