Ati*_*ziz 6 c# garbage-collection
我在实践中的观察结果GC.SuppressFinalize并不总能抑制对终结者的召唤.可能是终结器被调用了.我想知道是否GC.SuppressFinalize具有请求的性质而不是系统的保证?
更多信息
如果需要,以下信息可能有助于为问题提供更多背景信息.
该GC.SuppressFinalize文件总结确实状态是一个请求:
请求系统不为指定对象调用终结器.
我想知道这是偶然使用这个词还是真正用于描述运行时行为.
我已经SingletonScope从Schnell项目中看到了以下课程,该课程基于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上有一个完整的工作示例,其中包含编译说明,但请注意到目前为止无法确定地再现问题.
我多次使用完全相同的模式,GC.SupressFinalize 似乎总是有效。
请记住,调用 GC.ReRegisterForFinalize 将导致对象重新注册以进行终结。
每当我使用上述技术时,我总是确保在对象构造期间包含完整的堆栈跟踪,以便我可以追踪分配未处置对象的方法。
例如。在构造函数中使用
StackFrame frame = new StackFrame(1);
并在终结器期间在调试消息中报告这一点。
另外,我注意到你的 GC.SupressFinalize 不在finally子句中,如果在处理过程中抛出异常,你的对象终结器将不会被抑制。
| 归档时间: | 
 | 
| 查看次数: | 3491 次 | 
| 最近记录: |