Integrated: 8346195: Fix static initialization problem in GDIHashtable

Magnus Ihse Bursie ihse at openjdk.org
Tue Dec 17 15:51:42 UTC 2024


On Fri, 13 Dec 2024 14:30:37 GMT, Magnus Ihse Bursie <ihse at openjdk.org> wrote:

> There has been a latent problem in `GDIHashtable` since time immemorial, but due to sheer luck it has not caused any issues for us. However, I managed to provoke it when I was doing some build changes.
> 
> This is the problem:
> In `GDIHashtable`, there is a static field `GDIHashtable::BatchDestructionManager manager`, which is initialized in `GDIHashtable.cpp`.
> 
> In `AwtPen`, there is a static field `GDIHashtable cache`, which is initialized in `awt_Pen.cpp`.
> 
> The `GDIHashtable` constructor calls `manager.add(this)`.
> 
> For this to work, the manager must have been initialized prior to the AwtPen. However, the order of which static initializers are run between different compilation units are not well-defined, and we've just been lucky so far that it works.
> 
> This problem is known as the "Static Initialization Order Fiasco", see e.g. https://en.cppreference.com/w/cpp/language/siof
> 
> I have solved this by encapsulating the static manager instance in a method, which guarantees that it has been initialized before use. This seemed to me to be the cleanest solution.

This pull request has now been integrated.

Changeset: 03821d97
Author:    Magnus Ihse Bursie <ihse at openjdk.org>
URL:       https://git.openjdk.org/jdk/commit/03821d979b00ec60304ec0ba38b4755c773c6450
Stats:     14 lines in 2 files changed: 3 ins; 2 del; 9 mod

8346195: Fix static initialization problem in GDIHashtable

Reviewed-by: jwaters, prr

-------------

PR: https://git.openjdk.org/jdk/pull/22736


More information about the client-libs-dev mailing list