ice icon indicating copy to clipboard operation
ice copied to clipboard

Review _iceS_ static object

Open bernardnormier opened this issue 1 year ago • 0 comments

When slice2cpp generates code for a class or an exception, it generates one static instance of the first class/exception in the header file: e.g.

// Metrics.h
// For the first class or exception defined in Metrics.ice, in this case class Metrics.

/// \cond INTERNAL
static Metrics _iceS_Metrics_init;
/// \endcond

According to the comment slice2cpp's code, this is for:

    // We need an instance here to trigger initialization if the implementation is in a shared library.
    // But we do this only once per source file, because a single instance is sufficient to initialize
    // all of the globals in a shared library.

The comment does not seem accurate to me. We don't need an extra static in the header for the statics in the .cpp to get initialized in a shared library.

On the other hand, this could help with static builds: with this static in the header, including the header would ensure the statics in the .cpp are initialized even if you don't call anything else in the .cpp -- which is also a pretty rare use-case. Except it doesn't work now that the default ctor for generated classes and exceptions is "= default" i.e. inline. The construction of this static object doesn't need to execute code in the .cpp file and trigger the initialization of its statics.

As a test, I removed this static object with debug builds on macos, and the test-suite succeeds (shared and static). So we need to either:

  • figure out why this static is useful, update the comment and add a test
    • this could also require pytting the default ctor for generated classes and exceptions out of line
  • remove this static object and related code in slice2cpp

bernardnormier avatar Mar 17 '24 18:03 bernardnormier