실제로는 GC.SuppressFinalize
이 파이널 라이저에 대한 호출을 항상 억제하지는 않는다는 사실을 관찰했습니다. 그렇다고해서 파이널 라이저가 호출 될 수도 있습니다. 따라서 요청이 시스템이 아닌이 아닌 GC.SuppressFinalize
인지 여부가 궁금합니다.GC.SuppressFinalize는 보장됩니까?
추가 정보
다음 정보가 필요한 경우 후 질문에 대한 더 많은 컨텍스트를 제공하는 데 도움이 될 수 있습니다. 시스템이 지정된 개체에 대한 종료자를 호출하지
요청 :
GC.SuppressFinalize
문서 요약 요청입니다 상태를 않습니다.
나는 이것이 일상적으로 사용되는 단어인지, 아니면 실제로 런타임 동작을 설명하기위한 단어인지 궁금합니다.
Schnell 프로젝트에서 취한 을 기반으로 한 다음의 SingletonScope
클래스에서 이것을 관찰 해 보았습니다. 아이디어는 디버그 빌드에서 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에서 사용할 수 있지만 문제는 지금까지 결정 론적으로 복제 할 수 없습니다 수 있습니다 않습니다.
Trace in Dispose 메서드가 없으므로 finalizer 전에 성공적으로 호출되었다고 확신합니까? – Groo
@Groo : 예 C#을 사용하지 않는 한 확실합니다. :) –