opencv_dart icon indicating copy to clipboard operation
opencv_dart copied to clipboard

Disable native calls if an object is disposed.

Open fcunilim opened this issue 9 months ago • 3 comments

Describe the bug

Stepping through harmless code crashes the application.

To Reproduce

Just try the following code. Place a breakpoint on the first line and hit F10 to execute the code line by line until you reach (or try to reach) the third line:

    final image = cv.Mat.zeros(600, 800, cv.MatType.CV_8UC3);
    image.dispose();
    print('After dispose().');

Immediately after executing image.dispose(), using F10 on Visual Studio Code, the application crashes:

F/libc    (25616): Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x381c30bec81 in tid 25701 (1.ui), pid 25616 (ple.application)
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'motorola/sabahl_ge/sabahl:13/TLAS33.105-285-6/105-285-6:user/release-keys'
Revision: '0'
ABI: 'arm64'
Timestamp: 2025-03-09 19:36:01.551311735+0100
Process uptime: 53s
Cmdline: com.example.application
pid: 25616, tid: 25701, name: 1.ui  >>> com.example.application <<<
uid: 10266
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x00000381c30bec81
    x0  00000381c30bec81  x1  0000007500000000  x2  00000381c30bec81  x3  000000000000501a
    x4  000000750000a4b1  x5  0000000000000000  x6  000000750b786f41  x7  0000000000000002
    x8  00000381c30bec81  x9  00000074eb32fd88  x10 00000076e55718a0  x11 00000000ffffffff
    x12 0000000000000000  x13 0000000000000000  x14 00000075007ca010  x15 000000776b858d40
    x16 00000074ec425760  x17 00000074eb32fdac  x18 000000776b078000  x19 000000776b858d40
    x20 00000381c30bec81  x21 b4000076c9da1000  x22 0000007500008081  x23 000000750ba1d4d9
    x24 00000074eb32fd88  x25 000000776b679000  x26 b4000076c9da1000  x27 000000750c1f0290
    x28 0000000800000075  x29 000000776b858d30
    lr  00000074eb32fda0  sp  000000776b858d10  pc  00000074eb32fdb8  pst 0000000020000000
backtrace:
      #00 pc 000000000072fdb8  /data/app/~~8SwqNWiuFj86YZNj7aD4nw==/com.example.application-IfBt3mdq7GiPdmPK4aHc9w==/base.apk!libdartcv.so (cv::Mat::type() const+12) (BuildId: e01ba305a24c6a1dd51663920c3735f6f30ee3d1)
      #01 pc 000000000072fd9c  /data/app/~~8SwqNWiuFj86YZNj7aD4nw==/com.example.application-IfBt3mdq7GiPdmPK4aHc9w==/base.apk!libdartcv.so (cv_Mat_type+20) (BuildId: e01ba305a24c6a1dd51663920c3735f6f30ee3d1)
      #02 pc 00000000000075a4  [anon:dart-code]
Lost connection to device.

Exited.

I am getting the same problem using valid JPEG bytes with the following code:

    final image = cv.imdecode(jpegBytes, cv.IMREAD_UNCHANGED);
    image.dispose();

With the code above, I am getting a similar native stack trace ((cv::Mat::type() const+12) and (cv_Mat_type+20)).

Expected behavior

No crash.

Desktop (please complete the following information):

  • OS: Windows 11 24H2

Smartphone (please complete the following information):

  • Device: Android 13
  • ABI Version: arm64-v8a

This can be reproduced when running natively on Windows, too.

Using Flutter 3.27.3 (also tried 3.27.4), Visual Studio Code 1.98, Dart/Flutter extensions 3.106.

fcunilim avatar Mar 09 '25 18:03 fcunilim

I have an idea about what might be happening. When launching a debug session the VS Variables window is automatically expanded and the contents of the 'image' variable is displayed. After dispose() has been called, accessing the various getters causes a crash. Would it be possible for you to disable all native calls on an instance after dispose has been called? This seems a lot of hassle but this would be the only way to prevent crashes while debugging.

fcunilim avatar Mar 09 '25 20:03 fcunilim

I have an idea about what might be happening. When launching a debug session the VS Variables window is automatically expanded and the contents of the 'image' variable is displayed. After dispose() has been called, accessing the various getters causes a crash.

Exactly.

Would it be possible for you to disable all native calls on an instance after dispose has been called? This seems a lot of hassle but this would be the only way to prevent crashes while debugging.

Sounds possible but I have no idea how to implement it. Adding a new flag and checking whether an instance is disposed may works but some overheads will be introduced.

For now, you can check whether it's in debug mode and dispose it if not

final image = ...;

// do some process

if (!kDebugMode){
  image.dispose();
}

It doesn't solve you problem but much better and keeps the same behavior in release mode.

rainyl avatar Mar 10 '25 01:03 rainyl

Thanks, feel free to change this issue to enhancement (or close).

fcunilim avatar Mar 13 '25 10:03 fcunilim