gpt4 book ai didi

c# - GC.SuppressFinalize 有保证吗?

转载 作者:太空狗 更新时间:2023-10-29 21:25:55 25 4
gpt4 key购买 nike

我在实践中的观察是GC.SuppressFinalize并不总是抑制对终结器的调用。尽管如此,终结器可能还是被调用了。因此,我想知道 GC.SuppressFinalize 是否具有请求的性质,而不是系统的保证


更多信息

如果需要,以下信息可能有助于为问题提供更多背景信息。

GC.SuppressFinalize文档摘要确实声明这是一个请求:

Requests that the system not call the finalizer for the specified object.

我想知道这是随意使用该词还是真正旨在描述运行时行为。

我已经通过从 Schnell 中获取的以下 SingletonScope 类观察到这一点项目,基于 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 方法在第一行之后不使用任何实例变量。然后可以完成实例,即使 Dispose 仍在运行。

如果您在 Dispose 方法的末尾插入对 GC.KeepAlive(this) 的调用,您可能会发现问题所在离开。

Chris Brumme 有一个 blog post关于这个,我认为附近还有另一个……

关于c# - GC.SuppressFinalize 有保证吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/792660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com