Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upBug: CC0033 "Field should be disposed" is incorrectly raised on an assignment in a constructor #914
Comments
|
@Portikus Maybe you can consider use "using" statement. |
|
@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. |
|
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 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. |
|
@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. |
If I have a IDisposable field like:
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:
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.