Skip to content

Static initializers #20051

@jstaahl

Description

@jstaahl
System information (version)
  • OpenCV => 4.5.2
  • Operating System / Platform => host: macOS 64 bit, target iOS
  • Compiler => clang
Detailed description

Upon analyzing our iOS binary which links against opencv, we noticed a handful of static initializers (which must run before main) originating from opencv. All of them are attributed to the policy private static member of the templated SinglePolicy class:
opencv2/flann/any.h#L176.

These can be avoided by using the Meyer singleton pattern for the get_policy() function.

Steps to reproduce
  • Build an iOS app which links in the opencv library.
  • On the generated app, run:
otool -v -s __DATA __mod_init_func <path_to_main_binary_in_generated_app> \
  | awk '{print $2}' \                                                                                                                                                                                                                               
  | tail -n +3 \
  | xargs xcrun atos --arch arm64 -o <path_to_main_binary_in_generated_dSYM> -fullPath \
  | grep opencv

You will find:

__cxx_global_var_init (in <XYZ>) (.../opencv2/flann/any.h:173)
__cxx_global_var_init.8 (in <XYZ>) (.../opencv2/flann/any.h:173)
__cxx_global_var_init.9 (in <XYZ>) (.../opencv2/flann/any.h:173)
__cxx_global_var_init.10 (in <XYZ>) (.../opencv2/flann/any.h:173)
__cxx_global_var_init.11 (in <XYZ>) (.../opencv2/flann/any.h:173)
__cxx_global_var_init.12 (in <XYZ>) (.../opencv2/flann/any.h:173)
__cxx_global_var_init.13 (in <XYZ>) (.../opencv2/flann/any.h:173)
__cxx_global_var_init.14 (in <XYZ>) (.../opencv2/flann/any.h:173)
__cxx_global_var_init.15 (in <XYZ>) (.../opencv2/flann/any.h:173)

These symbols are listed in the __mod_init_func section which lists the symbols to run before executing the main function, and are therefore on the critical path for startup. They should not need to be constructed before the main function.

Here I propose using the Meyer singleton pattern to remove these static intializers: #20049

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 solution
  • I updated to 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