c# - Calling .Dispose() on a class that has a Finalizer -


according essential c# 6.0 should:

avoid calling dispose() on owned objects have finalizer. instead, rely on finalization queue clean instance.

  1. could please elaborate on i'm not clear on point of dispose if we're not call owned objects?
  2. apart reflection, how figure out if object has finalizer?
  3. how figure out when call close() / close() + dispose() other searching api documentation (if have access , exists) or reflection? see lot of questions around net specific types ( memorystream / form / sqlconnection / etc ) i'm looking more @ "how figure out yourself".

according dispose pattern should:

consider providing method close(), in addition dispose(), if close standard terminology in area. when doing so, important make close implementation identical dispose , consider implementing idisposable.dispose method explicitly.

but there times when should call both form, etc. questions "close , dispose - call?" close there's no defined approach apart

as usual answer is: depends. different classes implement idisposable in different ways, , it's necessary research.

edit: here full guideline, haven't asked permission reproduce since it's guideline (thereby assuming it's supposed freely shared public knowledge) , not part of actual training material, i'm hoping i'm not breaking rules.

guidelines
implement finalizer method on objects resources scarce or expensive, though finalization delays garbage collection.
implement idisposable support deterministic finalization on classes finalizers.
implement finalizer method on classes implement idisposable in case dispose() not invoked explicitly.
refactor finalization method call same code idisposable, perhaps calling dispose() method.
not throw exceptions finalizer methods.
call system.gc.suppressfinalize() dispose() avoid repeating resource cleanup , delaying garbage collection on object.
ensure dispose() idempotent (it should possible call dispose() multiple times).
keep dispose() simple, focusing on resource cleanup required finalization.
avoid calling dispose () on owned objects have finalizer. instead, rely on finalization queue clean instance.
avoid referencing other objects not being finalized during finalization.
invoke base class’s dispose() method when overriding dispose().
consider ensuring object becomes unusable after dispose() called. after object has been disposed, methods other dispose() (which potentially called multiple times) should throw objectdisposedexception.
implement idisposable on types own disposable fields (or properties) , dispose of said instances.

  1. could please elaborate on i'm not clear on point of dispose if we're not call owned objects?

without full text of book , context (i don't have copy of book, nor may many other people reading question), it's impossible sure mean. it's supposed book, , such have assume text you're quoted intended pertain only code in own finalizer. i.e. of course should dispose owned objects normally. in dispose() method.

it's if object hasn't been disposed properly. , answer there clean own unmanaged resources.

related now, advent (some time ago) of safehandle class, may not need finalizer @ all. instead, wrap own unmanaged resources in safehandle subclass , let class deal finalization.

  1. apart reflection, how figure out if object has finalizer?

apart reflection, you'd relying on source code (if available), documentation (if written), or fact object implements idisposable (i.e. make assumption…it's not guaranteed, there's strong correlation between two).

more point, note since possible correctly implement object implements idisposable without using finalizer (e.g. if use safehandle, or if implement idisposable can deterministically clean owned idisposable objects), presence of finalizer not guaranteed.

i think better way word guidance "don't dispose objects in finalizer". rely on fact idisposable object should somehow deal finalizing own owned resources, , focus on unmanaged resources own object owns directly.

  1. how figure out when call close() / close() + dispose() other searching api documentation (if have access , exists) or reflection? see lot of questions around net specific types ( memorystream / form / sqlconnection / etc ) i'm looking more @ "how figure out yourself".

you can't. not without inspecting code carefully. said…

you should never have call both close() , dispose(). 2 should equivalent, if class implemented correctly.

of course, there's nothing in .net enforces that. it's not possible sure wouldn't need to. if you're dealing type requires both, poorly written , may broken in other ways well. may best avoid using type altogether. :)

and of course, point out, form class in cases requires call both close() , dispose() (in cases, calling close() sufficient…it's because of weird way implemented modal dialogs exception rule). that's old api, designed before full implications of complexities of idisposable pattern understood. 1 hopes microsoft wouldn't design api same way today if had again (and indeed, wpf doesn't have same dichotomy).

modern implementations should better job of following conventions more uniformly.


addendum:

i did little browsing around. there are, of course, lots of articles gc, finalization, idisposable, finalizers, etc. on stack overflow, didn't see seemed directly equivalent question. 1 seemed closest though:

which objects can use in finalizer method?

others might useful additional reading:

when dispose method not called?
why call dispose()? memory leak won't occur?
idisposable , managed resources

and of course, classic:
proper use of idisposable interface


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -