Repository: opencv
Issue: Access violation under Windows 11 but not with Windows 10
Labels: bug,priority: low

### System Information

OpenCV version: 4.8.0 (but I could reproduce the issue with 4.7.0 and 4.8.1)
Operating System / Platform: Windows 10, Windows 11
Compiler & compiler version: GCC 12.3.0, Cmake 3.24.3 (mingw-64 distributed with Rtools43)

### Detailed description

I compiled OpenCV 4.8.0 using the same toolchain (GCC 12.3.0 + Cmake 3.24.3 from mingw-64 distributed with Rtools43. _Note: this is required for compiling R packages, I can't use anything else ATM_) under both Windows 10 and Windows 11. In both cases, the compilation was completed successfully and I could then also successfully compile code linking OpenCV. However, when running the resulting .exe, I get the expected result under Windows 10 _**but**_ I get an access violation under Windows 11. From VSCode debug: 

```
Exception thrown at 0x00007FFE0E5BED29 (libopencv_core480.dll) in main.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
```

Note that this is only happening on Windows 11 running on an x64 machine (also reported by other users of my R package). I could reproduce it on a physical x64 machine and an x64 VM. However, I could not reproduce it on an arm VM running Windows 11 (using VMWare Fusion on my Macbook M1). 

After scouring the web and not finding a single clue to help me solve this issue, I decided to post it here with the hope that someone will know what is happening. 

### Steps to reproduce

You will need to install the appropriate toolchain (Rtools43) from here: https://cran.r-project.org/bin/windows/Rtools/rtools43/rtools.html

I downloaded and extracted the OpenCV source code in a temporary folder (`C:/Users/R/AppData/Local/Temp/Rtmpwfh4SC/` in this case, but the exact location shouldn't matter). OpenCV was then compiled (only the core module, but adding other modules doesn't change anything) on all tested systems using the following line: 

```console
cmake -G \"Unix Makefiles\" -DCMAKE_C_COMPILER=C:/rtools43/x86_64-w64-mingw32.static.posix/bin/gcc.exe -DCMAKE_CXX_COMPILER=C:/rtools43/x86_64-w64-mingw32.static.posix/bin/g++.exe -DCMAKE_RC_COMPILER=C:/rtools43/x86_64-w64-mingw32.static.posix/bin/windres.exe -DCMAKE_MAKE_PROGRAM=C:/rtools43/usr/bin/make.exe -DENABLE_PRECOMPILED_HEADERS=OFF -DOpenCV_ARCH=x64 -DOpenCV_RUNTIME=mingw -DCMAKE_CXX_STANDARD=11 -DOPENCV_EXTRA_MODULES_PATH=C:/Users/R/AppData/Local/Temp/Rtmpwfh4SC/opencv_contrib-4.8.0/modules -DBUILD_LIST=core -DBUILD_opencv_world=OFF -DBUILD_opencv_contrib_world=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_TESTS=OFF -DOPENCV_ENABLE_ALLOCATOR_STATS=ON -DWITH_TBB=ON -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_C_FLAGS_RELEASE=\"-fstack-protector\" -DCMAKE_CXX_FLAGS_RELEASE=\"-fstack-protector\" -DCMAKE_INSTALL_PREFIX=C:\\Users\\R\\AppData\\Local\\R\\win-library\\4.3\\ROpenCV -BC:/Users/R/AppData/Local/Temp/Rtmpwfh4SC/opencv-4.8.0/build64 -HC:/Users/R/AppData/Local/Temp/Rtmpwfh4SC/opencv-4.8.0/
```

Here is a piece of code that will throw the exception on Windows 11 but not Windows 10

```cpp
#include <opencv2/core.hpp>

using namespace cv;

int main() {
  Mat blackImage = Mat::zeros(300, 400, CV_8UC3);
  Mat whiteImage = Mat::zeros(300, 400, CV_8UC3);
  add(blackImage, 255, whiteImage);
  return 0;
}
```

It was compiled using the following line: 

```console
C:\rtools43\x86_64-w64-mingw32.static.posix\bin\g++.exe -g C:\Users\R\Documents\Test\main.cpp -o C:\Users\R\Documents\Test\main.exe -std=c++11 -I C:\Users\R\AppData\Local\R\WIN-LI~1\4.3\ROpenCV\include -L C:\Users\R\AppData\Local\R\WIN-LI~1\4.3\ROpenCV\x64\mingw\bin -llibopencv_core480
```

I can also post the Cmake general configuration result if that helps. 

### 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)