Skip to content

Potential reference counting error in Python module #23059

Closed
@ksun212

Description

@ksun212

System Information

OpenCV python version: 4.6.0.66
Operating System / Platform: Ubuntu 20.04
Python version: 3.9.15

Detailed description

Hello! I have a Cpython version 3.9.15 built with NDEBUG flag. When I import cv2 in it, it aborted with garbage collection error.

Modules/gcmodule.c:116: gc_decref: Assertion "gc_get_refs(g) > 0" failed: refcount is too small
Enable tracemalloc to get the memory block allocation traceback

object address  : 0x55c596b9ad90
object refcount : 8
object type     : 0x55c596331780
object type name: type
object repr     : <class 'cv2.utils.nested.ExportClassName'>

Fatal Python error: _PyObject_AssertFailed: _PyObject_AssertFailed
Python runtime state: initialized

Current thread 0x0000149adc163600 (most recent call first):
  File "<frozen importlib._bootstrap>", line 228 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 1173 in create_module
  File "<frozen importlib._bootstrap>", line 565 in module_from_spec
  File "<frozen importlib._bootstrap>", line 666 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 986 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1007 in _find_and_load
  File "<frozen importlib._bootstrap>", line 1030 in _gcd_import
  File "/home/user/real_pure_python/cpython-3.9/Lib/importlib/__init__.py", line 127 in import_module
  File "/home/user/real_pure_python/cpython-3.9/my_python/lib/python3.9/site-packages/cv2/__init__.py", line 153 in bootstrap
  File "/home/user/real_pure_python/cpython-3.9/my_python/lib/python3.9/site-packages/cv2/__init__.py", line 181 in <module>
  File "<frozen importlib._bootstrap>", line 228 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 850 in exec_module
  File "<frozen importlib._bootstrap>", line 680 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 986 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1007 in _find_and_load
  File "<stdin>", line 1 in <module>
Aborted (core dumped)

I think about this for some time and guess the problem is that the class "ExportClassName" is added to two modules in "registerNewType", which calls "PyModule_AddObject", which decrease the reference count of "ExportClassName" by one in the end. Thus, before calling PyModule_AddObject, one should either hold an existing reference of "ExportClassName" or increase the reference by one.

Since I am new to opencv, there may be something I misunderstand. Thanks in advance.

Steps to reproduce

To reproduce this bug, import cv2 in any Cpython built with NDEBUG flag should be enough.

import cv2

Issue submission checklist

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions