-
Notifications
You must be signed in to change notification settings - Fork 150
NullReferenceException in ccrewrite-generated code #191
Comments
Thanks a lot! This is very useful bug report! |
Grabbed. |
Agreed 👍 |
I'd like to kindly ask, if there will be some fast-track updated release with that bug in mind? |
I'm still working on it, it was slightly more complicated that I've expexted. We can plan next release when this and other fixes would be ready. |
Win 7 / VS2015 public sealed class CodeContractsCracker
{
private List<Action> _actions = new List<Action>();
public CodeContractsCracker(Action action)
{
Contract.Requires<ArgumentNullException>(action != null);
this._actions.Add(() => action());
}
} Rewriter produces this output: public CodeContractsCracker(Action testAction)
{
CodeContractsCracker.CodeContractsCracker_<>c__DisplayClass1_0_0 codeContractsCracker_<>c__DisplayClass1_0_;
__ContractsRuntime.Requires<ArgumentNullException>(codeContractsCracker_<>c__DisplayClass1_0_.testAction != null, null, "testAction != null");
this._actions = new List<Action>();
base..ctor();
this._actions.Add(delegate
{
testAction();
});
} I saw that @SergeyTeplyakov is working on the problem. It would be great if you test updated CodeContracts against my example too. |
Also affecting our project. Repro similar to @a-vzhik on VS2015 against .Net 4.5.1.
@SergeyTeplyakov, do you know what would be needed for a fix? We would consider putting a dev on it since it is showing up in multiple spots in our solution. |
@mgaffigan I'm working on this fix right now. The bug is subtle but the fix should be relatively straightforward. I'll keep updating everyone. Hope to fix this stuff this week. |
@SergeyTeplyakov, excellent. Thank you. |
@SergeyTeplyakov, any luck? |
@mgaffigan Found root cause, working on the fix. |
And, just in case, if someone interested, the reason of this break is following: "Old" C# compiler has following code generation pattern for constructors: // VS2013-
Foo.ctor()
{
// Closure initialization
// Field initialization
// Base Constructor call
// Body of the constructor
} But in C# 6.0 the pattern was changed to: Foo.ctor()
{
// Field initialization
// Closure initialization
// Base Constructor call
// Body of the constructor
} So the method |
@mgaffigan I've made a dirty fix, will play around a little bit for more proper solution. Otherwise will create a PR with that... |
Found appropriate solution and real root cause of the issue. It so funny that after couple of hours of investigation you need to change 5 lines of code to fix some nasty bug:) PR submitted. I've added all test cases related to this issue. Everything works. |
@SergeyTeplyakov we recently faced this issue in our code. |
@tom-englert |
To reproduce, compile and run the following application in VS2015:
Observe a NullReferenceException thrown on the line with
Contract.Requires(a != null)
. (Initializing the Flag field is crucial for reproducing it; without the field or if the field is left uninitialized, the problem does not reproduce).When opened in a decompiler, the constructor shows the following code:
which explains the NullReferenceException.
FWIW, without the Flag field, the constructor looks like this:
The text was updated successfully, but these errors were encountered: