[ checkstyle-Bugs-3386855 ] False positive in Double checked locking

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[ checkstyle-Bugs-3386855 ] False positive in Double checked locking

SourceForge.net
Bugs item #3386855, was opened at 2011-08-05 08:39
Message generated for change (Comment added) made by oburn
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=397078&aid=3386855&group_id=29721

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Core Framework
Group: release 5.0
>Status: Closed
>Resolution: Accepted
Priority: 8
Private: No
Submitted By: Nagendra shiromani (shiromani)
Assigned to: Oliver Burn (oburn)
Summary: False positive in Double checked locking

Initial Comment:
Checkstyle analysis using version 5.4 is reporting Double checked locking error even if we try to use volatile keyword in the code.

Please check the following pages :

http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
http://en.wikipedia.org/wiki/Double-checked_locking

The documentation in the above pages suggest that the volatile keyword is introduced to resolve this error from JDK 1.5+ versions.

But Checkstyle is reporting the error even if we use volatile keyword in the code.

Code:
 
// Works with acquire/release semantics for volatile//
class Foo {
    private volatile Helper helper = null;
    public Helper getHelper() {
        Helper result = helper;
        if (result == null) {
            synchronized(this) {
                result = helper;
                if (result == null)
                    helper = result = new Helper();
            }
        }
        return result;
    }
 
    // other functions and members...
}

Output of Checkstyle execution:

java -jar checkstyle-5.4-all.jar -c checkstyle.xml Foo.java
Starting audit...
Foo.java:10:17:
 The double-checked locking idiom is broken and should be avoided.
Audit done.

 Is it a false positive in Checkstyle reporting? Please help us in resolving this error.

Thanks and Regards,
Nagendra Shiromani



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

>Comment By: Oliver Burn (oburn)
Date: 2012-04-16 04:44

Message:
You are correct - this is is a false positive.

I have removed the check from Checkstyle 5.6 moving forward.

I recommend using FIndBugs to reliably check for double checked locking.

changeset#:7a3753c7ddbd


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

Comment By: Daniel (tty0482)
Date: 2012-02-22 07:05

Message:
This is indeed a false positive. See the
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
linked from the description of the test on the CheckStyle homepage:

"JDK5 and later extends the semantics for volatile so that the system will
not allow a write of a volatile to be reordered with respect to any
previous read or write, and a read of a volatile cannot be reordered with
respect to any following read or write. See this entry in Jeremy Manson's
blog for more details.

"With this change, the Double-Checked Locking idiom can be made to work by
declaring the helper field to be volatile. This does not work under JDK4
and earlier."

Please fix the test.

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

Comment By: Nagendra shiromani (shiromani)
Date: 2011-11-22 08:00

Message:
Can someone please help us on this issue?

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

You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=397078&aid=3386855&group_id=29721

------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
Checkstyle-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/checkstyle-devel