Friday, 15 March 2013

c# - Will an IDisposable memory leak if you don't use a using statement? -



c# - Will an IDisposable memory leak if you don't use a using statement? -

will idisposable memory leak if don't utilize using statement? , if so, can provide memory leak illustration if not much code?

a correctly-written programme creates instance of type implements idisposable, , not specifically known capable of adequately cleaning after when abandoned, must ensure dispose called on instance before abandoning it. programme fails phone call dispose on type not known fine without broken.

although nice if automatic finalization take care of everything, it's pretty crummy cleanup mechanism. provides no guarantees regard sequencing, threading context, timeliness, or certainty of completion (when using deterministic cleanup, 1 can create reasonably programme won't appear finish if cleanup fails; when using finalization, programme may appear finish without attempting clean object).

microsoft may 1 time have intended every idisposable class should able adequately clean after if abandoned, not practical. in many cases, class effort clean after if abandoned add together massive amount of complexity, , turn broken programme have obvious problems easy track down, broken programme works except when timing of finalizer thread relative other thread causes things fail in unexpected , non-reproducible fashion.

there types which, despite implementing idisposable, unconditionally safe abandon, , there others may safely abandoned in circumstances. fine abandon such types in situations disposing them hard (e.g. because references held multiple objects manipulated various threads, , there's no nice way particular object can know when holds lastly surviving reference), provided 1 documents one's reasons believing such action safe , appropriate. such behavior not appropriate, however, in cases 1 has accepted idisposable objects of unknown lineage.

c# idisposable

No comments:

Post a Comment