Repository: opencv
Issue: Segmentation fault when mixing with Blender #371
Labels: bug,category: 3rdparty

### System Information

Operating system: Linux-5.19.0-50-generic-x86_64-with-glibc2.35 64 Bits, WAYLAND UI
Graphics card: NVIDIA GeForce RTX 4080/PCIe/SSE2 NVIDIA Corporation 4.6.0 NVIDIA 525.125.06
Blender Version: Version: 3.6.1 (3.6.2 tested as well)
OpenCV used in blender: 4.8 (last release - others tested as well)
libxkbcommon used in OpenCV: 1.4.1

### Detailed description

Segmentation fault using OpenCV as Blender script, but not as stand alone script

- Original post in blender foruns (please, take a look):
[https://projects.blender.org/blender/blender/issues/110842#issuecomment-993417](https://projects.blender.org/blender/blender/issues/110842#issuecomment-993417)

- Secondary post in libxkbcoomon (please, take a look):
[https://github.com/xkbcommon/libxkbcommon/issues/371](https://github.com/xkbcommon/libxkbcommon/issues/371)

### Steps to reproduce

Exact steps for others to reproduce the error

**PART ONE: Setup (after Blender install)**

1.1 - Install OpenCV for Blender Pyhon
`/home/me/blender/blender-3.6.1-linux-x64/3.6/python/bin$  ./python3.10 -m pip install opencv-contrib-python`

1.2 - Check insllation
`/home/me/blender/blender-3.6.1-linux-x64/3.6/python/bin$  ./python3.10 -m pip list`

Result:
```
Package                        Version
------------------------------ ---------
..............some ommited
opencv-contrib-python          4.8.0.74
..............some ommited
```

**PART TWO: Testing WebCam ruuning testOpenCV.py (attached) from Blender Python but still ouside Blender**
2.1 - Runing
`./python3.10 ../../../testOpenCV.py `

Result:

Frame openned with image refreshing
2.2 - Running with GDB
`/home/me/blender/blender-3.6.1-linux-x64/3.6/python/bin$  gdb ./python3.10 `

Result:
```
Frame openned with image refreshing
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./python3.10...
(gdb) r ../../../testOpenCV.py 
Starting program: /home/me/blender/blender-3.6.1-linux-x64/3.6/python/bin/python3.10 ../../../testOpenCV.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7fffe971d640 (LWP 44988)]
[New Thread 0x7fffe0f1c640 (LWP 44989)]
[New Thread 0x7fffe8f1c640 (LWP 44990)]
[New Thread 0x7fffe3fff640 (LWP 44991)]
[New Thread 0x7fffe37fe640 (LWP 44992)]
[New Thread 0x7fffe2ffd640 (LWP 44993)]
[New Thread 0x7fffe27fc640 (LWP 44994)]
[New Thread 0x7fffe1ffb640 (LWP 44995)]
[New Thread 0x7fffe17fa640 (LWP 44996)]
[New Thread 0x7fffc3fff640 (LWP 44997)]
[New Thread 0x7fffc37fe640 (LWP 44998)]
[New Thread 0x7fffc2ffd640 (LWP 44999)]
[New Thread 0x7fffc27fc640 (LWP 45000)]
[New Thread 0x7fffc1ffb640 (LWP 45001)]
[New Thread 0x7fffc17fa640 (LWP 45002)]
[New Thread 0x7fffc0ff9640 (LWP 45003)]
[New Thread 0x7fff9ffff640 (LWP 45004)]
[New Thread 0x7fff97fff640 (LWP 45005)]
[New Thread 0x7fff9f7fe640 (LWP 45006)]
[New Thread 0x7fff9effd640 (LWP 45007)]
[New Thread 0x7fff9e7fc640 (LWP 45008)]
[New Thread 0x7fff9dffb640 (LWP 45009)]
[New Thread 0x7fff9d7fa640 (LWP 45010)]
[New Thread 0x7fff9cff9640 (LWP 45011)]
[New Thread 0x7fff977fe640 (LWP 45012)]
[New Thread 0x7fff96ffd640 (LWP 45013)]
[New Thread 0x7fff967fc640 (LWP 45014)]
[New Thread 0x7fff95ffb640 (LWP 45015)]
[New Thread 0x7fff957fa640 (LWP 45016)]
[New Thread 0x7fff94ff9640 (LWP 45017)]
[New Thread 0x7fff5ffff640 (LWP 45018)]
Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
[New Thread 0x7fff57fff640 (LWP 45019)]

```

**PART THREE: Running inside Blender**

3.1 - Run Blender (no root privileges).

Scritping | New | copy testOpenCV.py | Run
Result:

Blender crashes
Crash file:
`cat /tmp/blender.crash.txt `
```
# Blender 3.6.1, Commit date: 2023-07-17 12:50, Hash 8bda729ef4dc

# backtrace

# Python backtrace
```
3.2 - Running with GDB
`/home/me/blender/blender-3.6.1-linux-x64/$  gdb ./blender `
```
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./blender...
(No debugging symbols found in ./blender)
(gdb) 
```
GDB runing causes segmentation fault
```
(gdb) r -P testOpenCV.py 
Starting program: /home/lveiga/Aplicativos/blender-3.6.1-linux-x64/blender -P testOpenCV.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7fffe77ff600 (LWP 45800)]
[New Thread 0x7fffe4790600 (LWP 45801)]
[New Thread 0x7fffe3f8f600 (LWP 45802)]
[New Thread 0x7fffe378e600 (LWP 45803)]
[New Thread 0x7fffe2f8d600 (LWP 45804)]
[New Thread 0x7fffe278c600 (LWP 45805)]
[New Thread 0x7fffe1f8b600 (LWP 45806)]
[New Thread 0x7fffe178a600 (LWP 45807)]
[New Thread 0x7fffe0f89600 (LWP 45808)]
[New Thread 0x7fffe0788600 (LWP 45809)]
[New Thread 0x7fffdff87600 (LWP 45810)]
[New Thread 0x7fffdf786600 (LWP 45811)]
[New Thread 0x7fffdef85600 (LWP 45812)]
[New Thread 0x7fffde784600 (LWP 45813)]
[New Thread 0x7fffddf83600 (LWP 45814)]
[New Thread 0x7fffdd782600 (LWP 45815)]
[New Thread 0x7fffdcf81600 (LWP 45816)]
[New Thread 0x7fffdc780600 (LWP 45817)]
[New Thread 0x7fffdbf7f600 (LWP 45818)]
[New Thread 0x7fffdb77e600 (LWP 45819)]
[New Thread 0x7fffdaf7d600 (LWP 45820)]
[New Thread 0x7fffda77c600 (LWP 45821)]
[New Thread 0x7fffd9f7b600 (LWP 45822)]
[New Thread 0x7fffd977a600 (LWP 45823)]
[New Thread 0x7fffd8f79600 (LWP 45824)]
[New Thread 0x7fffd8778600 (LWP 45825)]
[New Thread 0x7fffd7f77600 (LWP 45826)]
[New Thread 0x7fffd7776600 (LWP 45827)]
[New Thread 0x7fffd6f75600 (LWP 45828)]
[New Thread 0x7fffd6774600 (LWP 45829)]
[New Thread 0x7fffd5f73600 (LWP 45830)]
[New Thread 0x7fffd5772600 (LWP 45831)]
[New Thread 0x7fffd4f71600 (LWP 45832)]
[New Thread 0x7fffd3b0e600 (LWP 45834)]
[New Thread 0x7fffd2dff600 (LWP 45835)]
Read prefs: "/home/lveiga/.config/blender/3.6/config/userpref.blend"
[New Thread 0x7fffcb9ff600 (LWP 45836)]
[New Thread 0x7fffcabff600 (LWP 45837)]
[New Thread 0x7fffbe3fe600 (LWP 45838)]
[New Thread 0x7fffbcfff600 (LWP 45839)]
[New Thread 0x7fffbbf58600 (LWP 45840)]
[Thread 0x7fffbbf58600 (LWP 45840) exited]
[New Thread 0x7fffbbf58600 (LWP 45841)]
[New Thread 0x7fffbb1ff600 (LWP 45842)]
[Thread 0x7fffbbf58600 (LWP 45841) exited]
[Thread 0x7fffbb1ff600 (LWP 45842) exited]
[New Thread 0x7fffbb1ff600 (LWP 45843)]
[New Thread 0x7fffbbf58600 (LWP 45844)]
[Thread 0x7fffbb1ff600 (LWP 45843) exited]
[Thread 0x7fffbbf58600 (LWP 45844) exited]
[New Thread 0x7fffbbf58600 (LWP 45845)]
[New Thread 0x7fffbb1ff600 (LWP 45846)]
[New Thread 0x7fffb6dff600 (LWP 45847)]
[New Thread 0x7fffb65fe600 (LWP 45848)]
[New Thread 0x7fffb47ff600 (LWP 45849)]
[New Thread 0x7fffb3ffe600 (LWP 45850)]
[Thread 0x7fffb47ff600 (LWP 45849) exited]
[Thread 0x7fffb3ffe600 (LWP 45850) exited]
[New Thread 0x7fffb3ffe600 (LWP 45851)]
[New Thread 0x7fffb47ff600 (LWP 45852)]
[Thread 0x7fffb3ffe600 (LWP 45851) exited]
[Thread 0x7fffb47ff600 (LWP 45852) exited]
[New Thread 0x7fffb47ff600 (LWP 45853)]
[New Thread 0x7fffb3ffe600 (LWP 45854)]
[New Thread 0x7fffacbff600 (LWP 45855)]
[New Thread 0x7fffac3fe600 (LWP 45856)]
[New Thread 0x7fffab5ff600 (LWP 45857)]
[New Thread 0x7fffaa7ff600 (LWP 45858)]
[New Thread 0x7fffa99ff600 (LWP 45859)]
[New Thread 0x7fffa91fe600 (LWP 45860)]
[New Thread 0x7fffa83ff600 (LWP 45861)]
[New Thread 0x7fffa7bfe600 (LWP 45862)]
[New Thread 0x7fffa73fd600 (LWP 45863)]
[New Thread 0x7fffa65ff600 (LWP 45864)]
[New Thread 0x7fffa5dfe600 (LWP 45865)]
[New Thread 0x7fffa55fd600 (LWP 45866)]
[New Thread 0x7fffa4dfc600 (LWP 45867)]
[New Thread 0x7fffa45fb600 (LWP 45868)]
[New Thread 0x7fffa37ff600 (LWP 45869)]
[New Thread 0x7fffa2ffe600 (LWP 45870)]
[New Thread 0x7fffa27fd600 (LWP 45871)]
[New Thread 0x7fffa1ffc600 (LWP 45872)]
[New Thread 0x7fffa17fb600 (LWP 45873)]
[New Thread 0x7fffa0ffa600 (LWP 45874)]
[New Thread 0x7fffa07f9600 (LWP 45875)]
[New Thread 0x7fff9fff8600 (LWP 45876)]
[New Thread 0x7fff9f1ff600 (LWP 45877)]
[New Thread 0x7fff9e9fe600 (LWP 45878)]
[New Thread 0x7fff9e1fd600 (LWP 45879)]
[New Thread 0x7fff9d9fc600 (LWP 45880)]
[New Thread 0x7fff9d1fb600 (LWP 45881)]
[New Thread 0x7fff9c9fa600 (LWP 45882)]
[New Thread 0x7fff9c1f9600 (LWP 45883)]
[New Thread 0x7fff9adff600 (LWP 45884)]
[New Thread 0x7fff9a5fe600 (LWP 45885)]
[New Thread 0x7fff99dfd600 (LWP 45886)]
(3, 6, 1) Python Executable: /home/lveiga/Aplicativos/blender-3.6.1-linux-x64/3.6/python/bin/python3.10.
/home/lveiga/Aplicativos/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages /home/lveiga/Aplicativos/blender-3.6.1-linux-x64/3.6/python/bin/python3.10
[New Thread 0x7fff937ff600 (LWP 45887)]
[New Thread 0x7fff933fe600 (LWP 45888)]
[New Thread 0x7fff929ff600 (LWP 45889)]
[New Thread 0x7fff91fff600 (LWP 45890)]
[New Thread 0x7fff919fe600 (LWP 45891)]
[New Thread 0x7fff911ff600 (LWP 45892)]
[New Thread 0x7fff90dfe600 (LWP 45893)]
[New Thread 0x7fff903ff600 (LWP 45894)]
[New Thread 0x7fff8f9ff600 (LWP 45895)]
[New Thread 0x7fff8f5fe600 (LWP 45896)]
[New Thread 0x7fff8e5ff600 (LWP 45897)]
[New Thread 0x7fff8ddfe600 (LWP 45898)]
[New Thread 0x7fff813ff600 (LWP 45900)]
[New Thread 0x7fff80bfe600 (LWP 45901)]
[New Thread 0x7fff7fdff600 (LWP 45902)]
[New Thread 0x7fff7efff600 (LWP 45903)]
[New Thread 0x7fff7e1ff600 (LWP 45904)]
[New Thread 0x7fff7d3ff600 (LWP 45905)]
[New Thread 0x7fff7c3ff600 (LWP 45906)]
[New Thread 0x7fff7b9fe600 (LWP 45907)]
[New Thread 0x7fff7abff600 (LWP 45908)]
[New Thread 0x7fff79bff600 (LWP 45909)]
[New Thread 0x7fff791fe600 (LWP 45910)]
[New Thread 0x7fff781ff600 (LWP 45911)]
[New Thread 0x7fff777fe600 (LWP 45912)]
[New Thread 0x7fff765ff600 (LWP 45913)]
[New Thread 0x7fff75dfe600 (LWP 45914)]
[New Thread 0x7fff74fff600 (LWP 45915)]
[New Thread 0x7fff741ff600 (LWP 45916)]
[New Thread 0x7fff733ff600 (LWP 45917)]
[New Thread 0x7fff725ff600 (LWP 45918)]
[New Thread 0x7fff717ff600 (LWP 45919)]
[New Thread 0x7fff709ff600 (LWP 45920)]
[New Thread 0x7fff6fbff600 (LWP 45921)]
[New Thread 0x7fff6edff600 (LWP 45922)]
[New Thread 0x7fff6dfff600 (LWP 45923)]
[New Thread 0x7fff6d1ff600 (LWP 45924)]
[New Thread 0x7fff6c3ff600 (LWP 45925)]
[New Thread 0x7fff6b5ff600 (LWP 45926)]
[New Thread 0x7fff6a7ff600 (LWP 45927)]
[New Thread 0x7fff699ff600 (LWP 45928)]
[New Thread 0x7fff68bff600 (LWP 45929)]
[New Thread 0x7fff67dff600 (LWP 45930)]
Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
[New Thread 0x7fff627ff600 (LWP 45931)]

Thread 1 "blender" received signal SIGSEGV, Segmentation fault.
0x00007fff63405263 in find_atom_pointer () from /home/lveiga/Aplicativos/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages/cv2/qt/plugins/platforms/../../../../opencv_contrib_python.libs/libxkbcommon-x11-c65ed502.so.0.0.0
(gdb) 
```
GDB backtrace
```
(gdb) bt
#0  0x00007fff63405263 in find_atom_pointer ()
   from /me/blender/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages/cv2/qt/plugins/platforms/../../../../opencv_contrib_python.libs/libxkbcommon-x11-c65ed502.so.0.0.0
#1  0x00007fff6340548e in atom_intern () from /me/blender/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages/cv2/qt/plugins/platforms/../../../../opencv_contrib_python.libs/libxkbcommon-x11-c65ed502.so.0.0.0
#2  0x00007fff63404ff5 in xkb_keymap_new () from /me/blender/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages/cv2/qt/plugins/platforms/../../../../opencv_contrib_python.libs/libxkbcommon-x11-c65ed502.so.0.0.0
#3  0x00007fff63402caf in xkb_x11_keymap_new_from_device ()
   from /me/blender/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages/cv2/qt/plugins/platforms/../../../../opencv_contrib_python.libs/libxkbcommon-x11-c65ed502.so.0.0.0
#4  0x00007fff8fe8d558 in ?? () from /me/blender/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages/cv2/qt/plugins/platforms/../../../../opencv_contrib_python.libs/libQt5XcbQpa-5b2d853e.so.5.15.0
#5  0x00007fff8fe882a4 in QtOpenCVPython::QXcbConnection::QXcbConnection(QtOpenCVPython::QXcbNativeInterface*, bool, unsigned int, char const*) ()
   from /me/blender/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages/cv2/qt/plugins/platforms/../../../../opencv_contrib_python.libs/libQt5XcbQpa-5b2d853e.so.5.15.0
#6  0x00007fff8fe8ac87 in QtOpenCVPython::QXcbIntegration::QXcbIntegration(QtOpenCVPython::QStringList const&, int&, char**) ()
   from /me/blender/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages/cv2/qt/plugins/platforms/../../../../opencv_contrib_python.libs/libQt5XcbQpa-5b2d853e.so.5.15.0
#7  0x00007fffb4c0443f in ?? () from /me/blender/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages/cv2/qt/plugins/platforms/libqxcb.so
#8  0x00007fff8534da7c in QtOpenCVPython::QGuiApplicationPrivate::createPlatformIntegration() ()
   from /me/blender/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages/cv2/../opencv_contrib_python.libs/libQt5Gui-a7aedf18.so.5.15.0
#9  0x00007fff8534f020 in QtOpenCVPython::QGuiApplicationPrivate::createEventDispatcher() ()
   from /me/blender/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages/cv2/../opencv_contrib_python.libs/libQt5Gui-a7aedf18.so.5.15.0
#10 0x00007fff84cc82f6 in QtOpenCVPython::QCoreApplicationPrivate::init() () from /me/blender/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages/cv2/../opencv_contrib_python.libs/libQt5Core-39545cc7.so.5.15.0
#11 0x00007fff85351a4b in QtOpenCVPython::QGuiApplicationPrivate::init() () from /me/blender/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages/cv2/../opencv_contrib_python.libs/libQt5Gui-a7aedf18.so.5.15.0
#12 0x00007fff85d98979 in QtOpenCVPython::QApplicationPrivate::init() () from /me/blender/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages/cv2/../opencv_contrib_python.libs/libQt5Widgets-e69d94fb.so.5.15.0
#13 0x00007fff8967c0d7 in ?? () from /me/blender/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages/cv2/cv2.abi3.so
#14 0x00007fff896882dd in ?? () from /me/blender/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages/cv2/cv2.abi3.so
#15 0x00007fff8967635c in ?? () from /me/blender/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages/cv2/cv2.abi3.so
#16 0x00007fff88624cd3 in ?? () from /me/blender/blender-3.6.1-linux-x64/3.6/python/lib/python3.10/site-packages/cv2/cv2.abi3.so
#17 0x00000000008267fe in ?? ()
#18 0x000000000080116b in _PyObject_MakeTpCall ()
#19 0x000000000086211b in _PyEval_EvalFrameDefault ()
#20 0x000000000085bb02 in ?? ()
#21 0x0000000008371f02 in PyEval_EvalCode ()
#22 0x0000000008385814 in ?? ()
#23 0x000000000838579b in ?? ()
#24 0x00000000006a938d in ?? ()
#25 0x00000000006aa85c in PyRun_FileExFlags ()
#26 0x000000000141dbf5 in ?? ()
#27 0x00000000008aa6f5 in ?? ()
#28 0x000000000823a8f1 in ?? ()
#29 0x000000000070f2c3 in ?? ()
#30 0x00007fffe8029d90 in __libc_start_call_main (main=main@entry=0x70efe0, argc=argc@entry=3, argv=argv@entry=0x7fffffffe058) at ../sysdeps/nptl/libc_start_call_main.h:58
#31 0x00007fffe8029e40 in __libc_start_main_impl (main=0x70efe0, argc=3, argv=0x7fffffffe058, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffe048) at ../csu/libc-start.c:392
#32 0x00000000008a8bde in ?? ()
```



### Issue submission checklist

- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [X] I updated to the latest OpenCV version and the issue is still there
- [X] There is reproducer code and related data files (videos, images, onnx, etc)