★ wanayoo — archive 1999 https://github.com/code-cracker/code-cracker/issues/914Nouvelle recherche | Portail wanayoo
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: CC0033 "Field should be disposed" is incorrectly raised on an assignment in a constructor #914

Open
Portikus opened this issue Apr 20, 2017 · 5 comments

Comments

@Portikus
Copy link

@Portikus Portikus commented Apr 20, 2017 •

If I have a IDisposable field like:

private MemoryStream ms = new MemoryStream();

then I will get a CC0033 Warning that I should dispose this field, what is correct. But if I assign this field in my constructor like:

private readonly MemoryStream memoryStream;
public Class1()
{
     this.memoryStream = new MemoryStream();
}

Then CodeCracker is currently not raising any diagnostics.
As both are essentialy the same, a diagnostic should be raised.

We still should not raise a diagnostic when a field is a assigned on a method. We will raise it only when the assignment happens in a constructor.

@carloscds
Copy link
Contributor

@carloscds carloscds commented Apr 20, 2017

@Portikus Maybe you can consider use "using" statement.

@Portikus
Copy link
Author

@Portikus Portikus commented Apr 21, 2017 •

@carloscds That would be a fix for the not disposed object sure, but the analyser should detect such things because there is a disposable field which wont be disposed.

@giggio
Copy link
Member

@giggio giggio commented Jun 13, 2017

If you assign the field, then we understand that the lifecycle of the field is longer than the constructor/method. Because of that we can't tell if you should dispose it or not.

@giggio giggio closed this Jun 13, 2017
@giggio giggio added the wontfix label Jun 13, 2017
@Portikus
Copy link
Author

@Portikus Portikus commented Jun 14, 2017

@giggio I disagree. In my described example it is pretty the same if you use the first or the second code to create the UDPClient. But only one of it gets the warning. Furthermore of course should the field be disposed, the class create it so it is responsible to dispose it. Other analyzer like SonarAnalyzer.CSharp will trigger a warning for this code which say that the field 'client' should be disposed.

@giggio
Copy link
Member

@giggio giggio commented Jun 20, 2017

@Portikus Ok, I see your point now, and it makes sense. But only when the field is assigned on the constructor, which actually is the same as doing it on the field declaration. On a method, we should still not raise it.
I will update the description to make it more clear.

@giggio giggio reopened this Jun 20, 2017
@giggio giggio changed the title CC0033 "Field should be disposed" does not recognizes error if the field is assigned in constructor. Bug: CC0033 "Field should be disposed" is incorrectly raised on an assignment in a constructor Jun 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.