【发布时间】:2010-10-22 00:22:54
【问题描述】:
我在实践中的观察是GC.SuppressFinalize 并不总是抑制对终结器的调用。尽管如此,终结器可能会被调用。因此我想知道GC.SuppressFinalize是否具有请求的性质,而不是系统的保证?
更多信息
如果需要,以下信息可能有助于为问题提供更多背景信息。
GC.SuppressFinalize 文档摘要确实表明这是一个请求:
请求系统不调用 指定对象的终结器。
我想知道这是对这个词的随意使用还是真的打算描述运行时行为。
我观察到以下SingletonScope 类取自Schnell 项目,它基于original idea by Ian Griffiths,但更通用。这个想法是在调试版本中检测Dispose 方法是否被调用。如果没有,终结器最终会启动,并且可以发出警告。如果Dispose 被调用,那么GC.SuppressFinalize 应该阻止终结器触发。不幸的是,警告似乎无论如何都会触发,但不是以一种确定性的方式。也就是说,它们不会在每次运行时都开火。
#region License, Terms and Author(s)
//
// Schnell - Wiki widgets
// Copyright (c) 2007 Atif Aziz. All rights reserved.
//
// Author(s):
// Atif Aziz, http://www.raboof.com
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or (at
// your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
// License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#endregion
namespace WikiPad
{
#region Imports
using System;
using System.Diagnostics;
#endregion
//
// NOTE: To use SingletonScope and ISingletonScopeHelper with value
// types, use Nullable<T>. For example, if the type of value to scope
// is ThreadPriority then use ISingletonScopeHelper<ThreadPriority?>
// and SingletonScope<ThreadPriority?>.
//
//
// In debug builds, this type is defined as a class so a finalizer
// can be used to detect an undisposed scope.
//
/// <summary>
/// Designed to change a singleton and scope that change. After exiting
/// the scope, the singleton is restored to its value prior to entering
/// the scope.
/// </summary>
#if !DEBUG
internal struct SingletonScope<T, H>
#else
internal sealed class SingletonScope<T, H>
#endif
: IDisposable
where H : ISingletonScopeHelper<T>, new()
{
private T _old;
public SingletonScope(T temp)
{
_old = Helper.Install(temp);
}
private static H Helper
{
get { return new H(); }
}
public void Dispose()
{
//
// First, transfer fields to stack then nuke the fields.
//
var old = _old;
_old = default(T);
//
// Shazam! Restore the old value.
//
Helper.Restore(old);
#if DEBUG
GC.SuppressFinalize(this); // Only when defined as a class!
#endif
}
#if DEBUG
//
// This finalizer is used to detect an undisposed scope. This will
// only indicate that the scope was not disposed but (unfortunately)
// not which one and where since GC will probably collect much later
// than it should have been disposed.
//
~SingletonScope()
{
Debug.Fail("Scope for " + typeof(T).FullName + " not disposed!");
}
#endif
}
}
http://gist.github.com/102424 上提供了完整的工作示例以及编译说明,但请注意,到目前为止,该问题无法确定地重现。
【问题讨论】:
-
Dispose 方法中没有 Trace,我想你确定它在终结器之前被成功调用?
-
@Groo:是的,我敢肯定,除非在 C# 中使用被破坏。 :)
标签: c# garbage-collection