Is there a reason why C # /. NET compilers do not warn about Dispose ()?

I only thought about it today when I wrote IDisposable code.

It is good practice for the developer to either directly call Dispose() , or if the lifetime of the object allows you to use the using construct.

The only cases we need to worry about are those in which we cannot use using due to the mechanics of our code. But we should at some point call Dispose() on these objects.

Given that the C # compiler knows that an object implements IDisposable , it could theoretically also be known that Dispose() never called on it (it is a pretty smart compiler, as it is!). He may not know the semantics when a programmer should do this, but he can serve as a good reminder that he is never called because he was never used in the using construct, and the Dispose() method was never called directly, on any an object that implements IDisposable .

Any reason for this, or have thoughts to go down this route?

+4
source share
3 answers

theoretically one would also know that Dispose() never called on it

In some simple cases, he could determine that Dispose would never be called on him. It is impossible to determine, solely on the basis of static code analysis, that all created instances will be deleted. The code also does not have to be very complicated in order to get to the point where even assessing whether objects are left without a trace is simple.

To make matters worse, not all instances of IDisposable objects must be deleted. There may be several reasons for this. Sometimes an object implements IDisposable , although only a part of their instances actually does something in the implementation. ( IEnumerator<T> is a good example of this. A large number of implementations do nothing when they are located, but some do. If you know which particular implementation you have will never do anything at your disposal, you cannot worry if You do not know that you need to make sure that you call Dispose .

Then there are types like Task , which almost never need to be removed. (See Do I need to get rid of tasks?. ) In the vast majority of cases, you do not need to get rid of them, and it is useless to clutter up your code with using blocks or deletes calls that do nothing, interfere with readability.

+2
source

Code analysis rules will detect this. Depending on your version of VS, you can use FXCop or built-in analysis rules.

It requires static code analysis after compilation.

+1
source

The main rule regarding IDisposable is "whether the latter will leave the room, please turn off the light." One of the main drawbacks in developing most .NET languages ​​is the lack of a common syntax (or even an attribute tag) to indicate whether there will be code that contains a specific variable or class that contains a specific field:

  • Always be the last one to leave the room
  • Never be the last
  • Sometimes it happens that the latter leaves the room and easily knows at runtime whether this will be (for example, because the one who gave him the link said that).
  • Perhaps the latter will leave the room, but does not know before she leaves the room whether she will be the last.

If languages ​​had a syntax to distinguish between these cases, then it would be simple for the compiler to ensure that things that know they will be the last leave the room, go out with lights and things that were never the last to leave the room lights do not go out. The compiler or structure can facilitate the third and fourth scenarios if the framework includes types of wrappers that the compiler knew about. Regular reference counting is usually not good as the primary mechanism for determining when objects are no longer needed, since it takes every time a link is copied or destroyed to lock the processor, even if the copy owner knows that it will not be the last to exit rooms ", but variation on link counting is often the cheapest and most practical way to process script # 4 [copying a link should only increase the counter if both the original and the copy holders think h then they may be the last owner, and destroying a copy of a link should only decrease the counter if the link was increased when creating this copy].

In the absence of an agreement indicating whether the particular link should be considered "last in the room", there is no good way for the compiler to find out if the holder of this help should "turn off the light" (ie, call Dispose ). VB.NET and C # have a special using syntax for a specific situation where the owner of the variable knows that he will be the last to leave the room, but besides this, compilers cannot really require things to be cleaned if they do not understand them. C ++ / CLI has a more general syntax, but unfortunately it has many limitations on its use.

+1
source

All Articles