C# disposing IDisposable

后端 未结 7 2103
滥情空心
滥情空心 2021-01-14 02:44

Could someone explain what might happen if you don\'t Dispose some IDisposable entity (by using or direct Dispose cal

相关标签:
7条回答
  • 2021-01-14 03:32

    The most likely effects will be resource locking (e.g. you open a file handle and don't close it - eventually the finalizer will take care of it) or exhaustion (e.g. you do drawing operations and don't dispose of the objects).

    This can be a problem, especially with the latter issue, if the memory pressure isn't sufficient to cause a GC (which will kick off the finalization process) in time to use more of the resources. This leads to helpful errors as "a generic error occurred in GDI+."

    Poorly written objects may leak memory if they have only a Dispose method and no finalizer, but this is a rarity. Another possibility is that the Dispose method unsubscribes from static events. This could also be a significant problem and will "leak" memory.

    0 讨论(0)
提交回复
热议问题