|
SemmleCode Professional comes with a wide range of pre-packaged analyses, from architectural properties and metrics, to statement-level checks for likely bugs and violations of best practice.
Possibly Incorrect Lazy Initialization of a Static Field
This query finds code that initializes a non-volatile static field without synchronization. In a multi-threaded program, another thread might
start executing before the field is initialized, and will then see an incompletely initialized object.
How to Interpret the Query Results
The query flags every such initialization attempt and also displays the list of generated warnings in the result view.
How to Address the Query Results
The easiest solution is usually to make the surrounding method synchronized.
You could also declare the field volatile and use the
double-checked locking pattern. Notice, however, that this can lead to
quite subtle problems.
Source Code
References
Wikipedia article on the Double-Checked Locking Pattern
The "Double-Checked Locking is Broken" Declaration
|