I have a class that holds on to a list of unmanaged resources. These should be cleaned up in the finalizer, to make sure nothing is leaked. However, they are contained in a List<IntPtr>.
I have a couple of questions to this situation:
- Am I correct in assuming it is unsafe to enumerate the list in the finalizer, because it is possible that the list has already been garbage collected?
- Is the only other way to wrap each individual resource in a class that implements a finalizer, thus adding a much larger pressure on the garbage collector? And then the list would store this class?
I found the answer here: https://stackoverflow.com/a/20167998/553294. Here supercat states that:
So an object is considered reachable while in the finalization queue, which means the inner list is considered reachable as well. Thus, it is safe to access the list from the finalizer.