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.
source share