http://www.gotdotnet.com/team/fxcop/Docs/Rules/SecurityRules/IntptrAndFinalize.html

I struggle to comprehend the above FxCop rule.  Does it make a lick of
sense to anyone?

How could the finalizer decide to collect an object, if one or more
threads is currently executing code against it?  (Put simply: doesn't
the 'this' pointer count as a reachable reference, for the duration of
any instance-method call?)

I guess I can imagine some exotic scenarios (using unsafe code, etc)
where the last remaining ref to a managed object might be an IntPtr
field -- a scenario where, perhaps, the object is intended to handle
callbacks from unmanaged code or something.

But the proposed remedy -- adding a call to GC.KeepAlive(this) at the
bottom of any method which accesses the IntPtr field -- doesn't make a
lick of sense to me.

(a) The GC.KeepAlive(this) call wouldn't prevent the object from being
finalized right before an unmanaged callback came in -- unmanaged code
could potentially be calling back into a "dead" managed object.

(b) If GC.KeepAlive(this) really is necessary, ever, then it would
seem prudent and necessary to call it at the bottom of *every*
instance method, regardless of whether it has any IntPtr fields or
not...?

What am I missing?

Confused,
-Shawn
http://windojitsu.com

===================================
This list is hosted by DevelopMentor�  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to