.NET GC unrelated objects with static elements

So, I'm curious if GC - and especially finalization - will occur in an instance xin .NET when:

  • x does not refer to anything
  • x has a static property DbConnection
  • y is an instance of the same class as x
  • y still referring to something

It seems to me that in this case it xcan be declared dead by external links, but the completion of it can lead to the fact that it DbConnectionwill be deleted while yit still needs and contains a link to it.

So, what is this, in the scenario I described, assembled x? Does it end? Or all instances of this type need an orphan in a heap before any of them are collected? Will there be a completion on each instance?

I would have thought so, should the general rule be followed: Never touch a static or other common object in your finalizers?

+4
source share
1 answer

Yes, the copy xwill be eligible for GC. DbConnection, since it is static, is completely unrelated to instances in this case, and it does not matter to determine whether an object is suitable for the GC. It will not be received if the static property contains a link.

Never touch a static or other common object in your finalizers?

. , , , , ( ). SafeHandle , .

+5

All Articles