False +: DoubleCheckedLocking warning with volatile field
A source code analyzer
Brought to you by:
adangel,
juansotuyo
According to this article, double checked locking is safe in Java 5.0 if the field is volatile: http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html. However, the following code still generates a DoubleCheckedLocking warning.
public class Foo {
volatile Object baz;
Object bar() {
if (baz == null) { // baz may be non-null yet not fully created
synchronized (this) {
if (baz == null) {
baz = new Object();
}
}
}
return baz;
}
}
Is there any differentiation between Java versions of the source code?
Older versions do not allow this safely.
In the Java 5 and later, the volatile modifier of the member variable is needed.
I'll work on this one and the other one on DCL.
I reproduce and fixed this issue. From now on, DCL rule will ignore DCL match that return a volatile typed field. The fix will be available in the next 4.2.6 release and the 5.0.x series also.
Thanks for your report.