I am trying to understand how .NET Code Contracts interact with the lock keyword using the following example:
public class TestClass { private object o1 = new object(); private object o2 = new object(); private void Test() { Contract.Requires(this.o1 != null); Contract.Requires(this.o2 != null); Contract.Ensures(this.o1 != null); lock (this.o2) { this.o1 = new object(); } } }
When I run the contract code static analysis tool, it prints a warning: Ensures unproven: this.o1 != null
If I do:
- change
o2 to lock to o1 , - change
o1 inside the lock block to o2 , - add second line inside
lock block assigning new object to o2 - change
lock (this.o2) to if (this.o2 != null) , - completely remove the
lock statement.
warning disappears.
However, changing the line inside the lock block to var temp = new object(); (thus removing all references to o1 from the method) still raises a warning:
private void Test() { Contract.Requires(this.o1 != null); Contract.Requires(this.o2 != null); Contract.Ensures(this.o1 != null); lock (this.o2) { var temp = new object(); } }
So there are two questions:
- Why does this warning occur?
- What can be done to prevent this (bearing in mind that this is an example of a toy, but in fact, everything happens inside the
lock in real code)?
Philip c
source share