Why does FxCop flag GC.KeepAlive() as a violation?

2019-07-18 19:16发布

what is so bad about GC.KeepAlive() that FxCop flags this as a violation?

2条回答
看我几分像从前
2楼-- · 2019-07-18 19:48

Probably because it's considered bad practice to be calling it, just like it's a generally a bad idea to call GC.Collect -- it has generally negative consequences on the collector and/or is a possible indication of a design flaw on your end.

You should be able to reconfigured FxCop to not consider calls to the method a problem if you need to call it, however. There are valid reasons to do so, after all.

查看更多
Root(大扎)
3楼-- · 2019-07-18 20:02

It's not that GC.KeepAlive is particularly bad, but rather that it's not necessary if you convert to SafeHandle usage, and you really, really ought to convert to SafeHandle usage. If you're not sure why, you might want to consider taking a look at http://blogs.msdn.com/b/bclteam/archive/2005/03/16/396900.aspx and http://blogs.msdn.com/b/bclteam/archive/2006/06/23/644343.aspx.

查看更多
登录 后发表回答