Search code examples
c#volatilecode-contracts

MissingFieldException with CodeContracts


Once I turned on code rewriter for Code Contracts, I started to see an issue with code that used to work before.

I have a protected volatile bool member field that was visible from other assemblies in classes that inherit from mine. After enabling ccrewrite on my assembly, accesses to that field are now throwing a MissingFieldException.

I know that it is bad style to expose fields in this fashion. Once I change to expose a property and/or remove volatile marker, problem goes away.

I don't currently have 100% code coverage.

My question is, how can I still rely on Code Contracts, but have my code working as it did before? How can I find all places where the behavior changed?

Code Contracts version 1.4.50327.0

Microsoft (R) .NET Contract Rewriter Version 1.4.50327.0

EDIT There has been some work done in Code Contracts, but the release notes aren't awesome. Specifically, it's not clear what's been fixed. I am quoting:

Release 1.4.51019.0 (October 19, 2012) Fixed more problems with the task manager in VS2012. Fixed a bug in CCDoc. Other miscellaneous bug fixes.

Release 1.4.50910.0 (September 10, 2012) Fixes the task manager so it runs correctly in VS2012: static checker now runs in the background, squigglies are drawn, and contract suggestions appear in the Error List. Updated the installer so that "devenv /setup" no longer needs to be run manually after installation. It is now done automatically as part of the installation. (Which makes the installation take a lot longer...) Fixes several bugs in CCDoc.

Release 1.4.50813.1 (August 13, 2012) Bug fixes (everywhere!). Performance improvements (static checker). Precision improvements (static checker). Fixes to installer and build files.

Release 1.4.50327.0 (March 27, 2012) VS2012 support! (But you need to run "devenv /setup" manually after installation!) Bug fixes.


Solution

  • I fixed this in the internal release. It will appear in the next release of CodeContracts.

    EDIT Here are the release notes from Code Contracts site:

    Release 1.5.60502.11 (May 2, 2013)

    • Build script fix to not warn about missing contract reference assembly for C/C++ interop projects
    • Fix to VB constructor extraction
    • Properly handle generic methods in interface contracts when called from contracts.
    • Fix handling of unbox in non-null analysis (avoid a null deref)
    • Better handling of nullable and boxing constraints in static checker
    • Fix to show setter contracts on F12 (goto metadata)
    • More robust checking in implementation of Editor Extension
    • Reduced memory consumption and startup time of ccrewrite
    • Handle MEF Import attributes in non-null analysis
    • Preserve cross-assembly access to volatile fields in rewriter.
    • Fix a bug in the contract extractor when closures were present in a constructor.
    • Fix bug in rewriter when abbreviator method had static generic closure method.
    • Support async on pre 4.5 versions of the framework.