Repository: opencv
Issue: OpenCVLoader.initDebug() causes SIGSEGV in libart.so between onPause and onResume in debug sessions (simple repro included)
Labels: bug,platform: android,needs investigation

<!--
If you have a question rather than reporting a bug please go to https://forum.opencv.org where you get much faster responses.
If you need further assistance please read [How To Contribute](https://github.com/opencv/opencv/wiki/How_to_contribute).

This is a template helping you to create an issue which can be processed as quickly as possible. This is the bug reporting section for the OpenCV library.
-->

##### System information (version)
<!-- Example
- OpenCV => 4.6.0
- Operating System / Platform => Android 7.1.1 (Sony Xperia Z5 Compact)
- Compiler => Android Studio 2021.2.1 Patch 2
-->

- OpenCV => 4.6.0
- Operating System / Platform => Android 7.1.1 (Sony Xperia Z5 Compact)
- Compiler => Android Studio 2021.2.1 Patch 2 on Windows 11

##### Detailed description

Calling OpenCVLoader.initDebug() once at the beginning of an application succeeds, but a SIGSEGV will subsequently be raised in libart.so everytime a screen rotation occurs. The SIGSEGV will be raised in between onPause and onResume calls for the fragment.

##### Steps to reproduce

<!-- to add code example fence it with triple backticks and optional file extension
    ```.cpp
    // C++ code example
    ```
 or attach as .txt or .zip file
-->

- Open Android Studio 2021.2.1 Patch 2
- File > New > New Project... > Phone and Tablet > select 'Basic Activity'. Make sure the language is set to Java.
- Download OpenCV 4.6.0 (latest release) from [sourceforget.net](https://sourceforge.net/projects/opencvlibrary/files/4.6.0/opencv-4.6.0-android-sdk.zip/download) and unzip the 'sdk' directory next to the settings.gradle file (also next to the 'app' folder of the application) as a folder named 'opencv' (not 'sdk')
- File > New > Import Module... > source directory : the 'opencv' directory mentionned above
- Give a name to the opencv module, for instance 'opencv', and finish the import procedure. We now have two projects in the IDE, :app and :opencv
- Open settings.gradle and reverse the order of these two lines (for some reason, they are in the wrong order when the project is imported):

    ```
    project(':opencv').projectDir = new File(rootDir, 'opencv/')
    include 'opencv'
    ```
- get rid of the kotlin extension declaration, as the project is written in Java
- Build the project and launch a debug session on a Sony Xperia Z5 Compact (E5823) device with Android 7.1.1 Nougat. The application is launched successfully
- The first fragment shows up, now rotate the screen, everything is fine.
- Exit the application
- Override FirstFragment's onCreate method by adding this code:

    ```
    @Override
    public void onCreate(Bundle b) {
      super.onCreate(b);
      boolean success = OpenCVLoader.initDebug();
    }
    ```

- Following this edit, the IDE complains OpenCVLoader is not a recognized class - follow the suggestions to import the '........._main' module and then do 'Import class'. Now the class is recognized and everything compiles fine.
- Launch a new debug session
- 'success' will be true, above, as can be seen with the debugger if need be
- Now rotate the device's screen
- A **SIGSEGV occurs after onPause() has been called.**, and the debugger is entered

    ```
    SIGSEGV (signal SIGSEGV: invalid address (fault address: 0x0))
    libart.so: art_sigsegv_fault
    ```

    ```
	<unknown> 0x0000007f92a99904
	art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*) 0x0000007fb0cba304
	art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, art::DexFile::CodeItem const*, art::ShadowFrame*, art::JValue*) 0x0000007fb0e6a5b0
	bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*) 0x0000007fb0e6358c
	bool art::interpreter::DoInvoke<(art::InvokeType)2, false, false>(art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*) 0x0000007fb0e92fdc
	art::JValue art::interpreter::ExecuteSwitchImpl<false, false>(art::Thread*, art::DexFile::CodeItem const*, art::ShadowFrame&, art::JValue, bool) 0x0000007fb0e86cb4
	art::interpreter::Execute(art::Thread*, art::DexFile::CodeItem const*, art::ShadowFrame&, art::JValue, bool) 0x0000007fb0e3c950
	art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::DexFile::CodeItem const*, art::ShadowFrame*, art::JValue*) 0x0000007fb0e42648
	bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*) 0x0000007fb0e63570
	bool art::interpreter::DoInvokeVirtualQuick<false>(art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*) 0x0000007fb0e80b74
	art::JValue art::interpreter::ExecuteSwitchImpl<false, false>(art::Thread*, art::DexFile::CodeItem const*, art::ShadowFrame&, art::JValue, bool) 0x0000007fb0e87938
	...
	[lots of lines]
	...
	art::InvokeWithVarArgs(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, _jmethodID*, std::__va_list) 0x0000007fb1004d14
	art::JNI::CallStaticVoidMethodV(_JNIEnv*, _jclass*, _jmethodID*, std::__va_list) 0x0000007fb0f20b00
	<unknown> 0x0000007fb3305278
	android::AndroidRuntime::start(char const*, android::Vector<android::String8> const&, bool) 0x0000007fb3307890
	<unknown> 0x0000005570287264
	__libc_init 0x0000007fb2bbf89c
    ```

- Carrying execution with the Continue button works fine (surprisingly ?)
- onResume() is called and execution resumes normally.

Any subsequent screen rotation will result in a SIGSEGV.

This is very annoying when debugging on the device, which I always prefer.

At the beginning, I thought it was a problem with my code, so I posted a message on stackoverflow:

[SIGSEGV libart.so exception only occuring on one computer when debugging, not on the other](https://stackoverflow.com/questions/73486304/sigsegv-libart-so-exception-only-occuring-on-one-computer-when-debugging-not-on)

Because I managed to build a simple repro, I am posting this issue here.

Surprisingly, there is one computer on which I don't experience the problem, but it is an older Android Studio version (2021.2.1 with no patch).

##### Issue submission checklist

 - [x] I report the issue, it's not a question
   <!--
   OpenCV team works with forum.opencv.org, Stack Overflow and other communities
   to discuss problems. Tickets with questions without a real issue statement will be
   closed.
   -->
 - [x] I checked the problem with documentation, FAQ, open issues,
       forum.opencv.org, Stack Overflow, etc and have not found any solution
   <!--
   Places to check:
   * OpenCV documentation: https://docs.opencv.org
   * FAQ page: https://github.com/opencv/opencv/wiki/FAQ
   * OpenCV forum: https://forum.opencv.org
   * OpenCV issue tracker: https://github.com/opencv/opencv/issues?q=is%3Aissue
   * Stack Overflow branch: https://stackoverflow.com/questions/tagged/opencv
   -->
 - [x] I updated to the latest OpenCV version and the issue is still there
   <!--
   master branch for OpenCV 4.x and 3.4 branch for OpenCV 3.x releases.
   OpenCV team supports only the latest release for each branch.
   The ticket is closed if the problem is not reproduced with the modern version.
   -->
 - [x] There is reproducer code and related data files: videos, images, onnx, etc
   <!--
   The best reproducer -- test case for OpenCV that we can add to the library.
   Recommendations for media files and binary files:
   * Try to reproduce the issue with images and videos in opencv_extra repository
     to reduce attachment size
   * Use PNG for images, if you report some CV related bug, but not image reader
     issue
   * Attach the image as an archive to the ticket, if you report some reader issue.
     Image hosting services compress images and it breaks the repro code.
   * Provide ONNX file for some public model or ONNX file with random weights,
     if you report ONNX parsing or handling issue. Architecture details diagram
     from netron tool can be very useful too. See https://lutzroeder.github.io/netron/
   -->