Questions tagged [suppressfinalize]

This method sets a bit in the object header of obj, which the runtime checks when calling finalizers. Objects that implement the IDisposable interface can call this method from the object's IDisposable.Dispose implementation to prevent the garbage collector from calling Object.Finalize on an object that does not require it.

This method sets a bit in the object header of obj, which the runtime checks when calling finalizers.

Objects that implement the IDisposable interface can call this method from the object's IDisposable.Dispose implementation to prevent the garbage collector from calling Object.Finalize on an object that does not require it.

Doc: https://docs.microsoft.com/en-us/dotnet/api/system.gc.suppressfinalize

18 questions
315
votes
5 answers

When should I use GC.SuppressFinalize()?

In .NET, under which circumstances should I use GC.SuppressFinalize()? What advantage(s) does using this method give me?
Sam Saffron
  • 121,058
  • 74
  • 309
  • 495
31
votes
5 answers

What's the purpose of GC.SuppressFinalize(this) in Dispose() method?

I have the following code: public void Dispose() { if (_instance != null) { _instance = null; // Call GC.SupressFinalize to take this object off the finalization // queue and prevent finalization code for this object…
mr.b
  • 4,771
  • 11
  • 33
  • 53
30
votes
5 answers

Why should we call SuppressFinalize when we don't have a destructor

I have few Question for which I am not able to get a proper answer . 1) Why should we call SuppressFinalize in the Dispose function when we don't have a destructor . 2) Dispose and finalize are used for freeing resources before the object is garbage…
somaraj
  • 385
  • 1
  • 4
  • 6
16
votes
5 answers

C# language: Garbage Collection, SuppressFinalize

I'm reading "The C# Language", 4th Edition, it talks about garbage collection as below: "BILL WAGNER: The following rule is an important difference between C# and other managed environments. Prior to an application’s termination, destructor's for…
athos
  • 5,550
  • 3
  • 40
  • 81
11
votes
5 answers

IDisposable GC.SuppressFinalize(this) location

I use a default IDisposable implementation template (pattern) for my code. snippet: public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool isDisposing) { if (!this.disposed) { …
Patrick Peters
  • 9,103
  • 7
  • 53
  • 99
6
votes
3 answers

ReRegisterForFinalize SuppressFinalize real life example

I was just reading this article, “Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework”, by Jeffrey Richter, and I couldn't think of any real life sample for using ReRegisterForFinalize or SuppressFinalize. Could anyone…
Nahum
  • 6,431
  • 10
  • 45
  • 69
6
votes
1 answer

Calling SuppressFinalize multiple times

Is there any downside of calling GC.SuppressFinalize(object) multiple times? Protected Dispose(bool) method of the dispose pattern checks whether it is called before but there is no such check in the public Dispose() method. public void Dispose() { …
Şafak Gür
  • 6,389
  • 3
  • 52
  • 87
4
votes
1 answer

May I have a case where GC.SuppressFinalize has any substantial effect please?

Yes, I know how to use GC.SuppressFinalize() - it's explained here. I've read many times that using GC.SuppressFinalize() removes the object from finalization queue and it's assumed this is good because it relieves the GC from extra work calling the…
sharptooth
  • 159,303
  • 82
  • 478
  • 911
3
votes
3 answers

Overhead of having a Finalizer - with/without SuppressFinalize in Dispose

Assuming the following: A class has managed only members. Some members implement IDisposable. The class is sealed - a class can't derive from and add unmanaged resources. The object is used inside a using statement - i.e Dispose() is called when…
Ricibob
  • 7,097
  • 3
  • 39
  • 61
2
votes
1 answer

Why can we still use a disposed object?

I have DisposedPatterenDemo class inherit from IDisposable. I have Disposed the object of this class and then after it tries to call the Method() of the same class, I am getting returned value from Method but obj is already disposed of. var…
2
votes
2 answers

Should I implement GC.SupressFinalize on IDisposable AND Finalize?

The code review checklist in my new client place has the following - Class implementing Dispose and Finalize should have a call to GC.SupressFinalize in Dispose implementation Why? Should it not read as Class implementing IDisposable interface…
Kanini
  • 1,777
  • 4
  • 29
  • 56
2
votes
0 answers

Is this how to "Implement IDisposable correctly"?

I ran Code Analysis on one of my projects, and it gave me two finger wags, namely: CA1063 Implement IDisposable correctly Provide an overridable implementation of Dispose(bool) on 'UserStore' or mark the type as sealed. A call to Dispose(false)…
B. Clay Shannon
  • 1,055
  • 124
  • 399
  • 759
2
votes
1 answer

How should Dispose be coded in classes that implement IDependencyResolver and IDependencyScope?

I ran Code Analysis on my Web API project, in which I'm trying to implement IoC and DI using Castle Windsor, and it found four problems. The four things it found were all in WindsorDependencyResolver, and all four are "Implement IDisposable…
1
vote
1 answer

what class calls SuppressFinalize in its constructor such that your dispose call is useless?

There are some classes such as DataTable who already called SuppressFinalize in its constructor and so there is no point to call dispose/use using on it. (because dispose is for releasing earlier but there is nothing to release) So, I want to know a…
ill mg
  • 291
  • 3
  • 8
1
vote
1 answer

In the TimedLock why is SuppressFinalize(tl.leakDetector) needed?

http://www.interact-sw.co.uk/iangblog/2004/04/26/yetmoretimedlocking Why is this line needed? System.GC.SuppressFinalize(tl.leakDetector); I would have thought the finalizer should not be suppress to run the code that alerts to a left over…
CodingThunder
  • 853
  • 2
  • 9
  • 12
1
2