IDisposable with destructor: requires a thread-safe implementation?

This is pretty much just for me to make sure I'm right:

We have a resource class that implements the IDisposal template. It should (by design) be implemented in a way that allows it to receive a call more than once (even if we try to call it exactly once, of course). We also implement a finalizer that also calls the Dispose () method - just like backing up. If called manually, Dispose () will also call GC.SuppressFinalize (this).

There are several examples of removal schemes. Most of them call GC.SuppressFinalize (this) at the end of the delete code. Some argue that it would be better to name it at the beginning of the Dispose () method, before any cleanup. The latter argue that this will ensure that the GC does not call the finalizer at the same time while we are still cleaning.

Question:
It seems that placing GC.SuppressFinalize at the beginning is not better? We still have a race condition, right? Is it true that we should instead implement Dispose () in streaming safe mode?

+3
source share
3 answers

, , , , . GC.SuppressFinalize(this) 'this', , GC.SuppressFinalize. , , Dispose, , Dispose, - ( , - ) .

, , , . Microsoft . , . , ( ), , .

+2

GC , .

, , - "", this . , dispose , .

, , SuppressFinalize .

, CLR , / . "" , , - , , JIT, , .

.

+4

- .

, Dispose , , , . , , .

microsoft GC.SuppressFinalize Dispose.

+1
source

Source: https://habr.com/ru/post/1785857/


All Articles