how to ensure no memory leak in the following case - c#

If a class has a property which contains unmanaged resources. How to make sure no memory leak when using the class
Class A
{
B {get; set;}
}
B contains unmanaged resources.

Implement IDisposable and clean up your unmanaged resources by calling Dispose(), preferably placing the call to Dispose in a finally statement so you clean up resources even in the case of an exception.
C# has a using keyword that you can employ to make sure that the Dispose method is called, even if an exception is thrown.
EDIT: Incorporated call to GC.SuppressFinalize and finalizer implementation per Ran's answer
class A : IDisposable
{
private bool _disposed;
~A()
{
this.Dispose(false);
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
// dispose managed resources
}
// clean up unmanaged resources
_disposed = true;
}
}
}
class Program
{
static void Main(string[] args)
{
using (var someInstance = new A())
{
// do some things with the class.
// once the using block completes, Dispose
// someInstance.Dispose() will automatically
// be called
}
}
}

Using IDisposable might not be enough, because it relies on the user remembering to call Dispose or to use using etc.
For a complete solution, combine IDisposable and a finalizer. Like this:
Edit: Made some corrections in the Dispose method based on SpeksETC's comment.
class MyClass : IDisposable
{
~MyClass()
{
Dispose(false);
}
public void Dispose()
{
GC.SupressFinalize();
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (!disposing)
{
// clear unmanaged resources here (can only be called once)
...
}
// dispose called explicitly by the user, clean up managed resources here
...
}
}
This ensures that native resources will always be cleared, even if the user forgets to call Dispose, while still allowing the user to clear the resources early.
The if inside the Dispose implementation is needed because if this class is being finalized, you may not call Dispose on your members because they may have been GC'ed already.

I think it is important to point out that B is a managed resource that only contains unmanaged resources.
Therefore, A should implement IDisposable and dispose of B in its Dispose() method, but does not need a finalizer since it doesn't have any unmanaged resources to clean up - the finalizer needs to be implemented within B itself. Even if you implemented a finalizer it would call a Dispose that looked like this:
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// dispose called explicitly by the user, clean up managed resources here
b.Dispose()
}
// no unmanaged resources to clean up so do nothing which makes Finalizer unneccesary
...
}

SpeksEtc is correct in that if B contains unmanaged resources then B should ensure there are no leaks not A.
But what are the unmanaged resources and would the SafeHandle class help? Then B would contain a SafeHandle which would contain the unmanaged resource and you wouldn't need to worry about it.

Related

Is it possible to force C# finalizers be run in .NET Core on program exit?

I have a .NET Core C# class that wraps an unmanaged pointer and it should be freed on program exit along with other resource cleanup. However, the destructor is not being called. I have tried in both Debug and Release mode. I see that .NET Core apparently doesn't guarantee that destructors will be run, so what is a recommended workaround? IMO the main point of garbage collection is to avoid having the developer track references, so I find this behavior surprising, to say the least.
From MSDN: In .NET Framework applications (but not in .NET Core applications), finalizers are also called when the program exits.
public Demo {
IntPtr _ptr;
public Demo()
{
Console.WriteLine("Constructor");
_ptr = /* P-invoke external function */
~Demo
{
Console.WriteLine("Destructor");
/*P-invoke ptr deletion */
}
}
public static void Main()
{
Demo demo = new Demo();
demo = null;
GC.Collect();
}
Program output:
Constructor
<...>\Test.exe (process 7968) exited with code 0.
More changes are needed to improve the likelihood that the finalizer will be called.
Btw, Finalizer is never guaranteed to be called. If you want to gurantee the resources release, implement IDisposable and call Dispose() before the app/method/code block exit. Additionally to make Dispose() guaranteed to call (even if app crashes, except FailFast and StackOverflow) before exiting the code block, use try-finally or using statements.
Here's an example to play with.
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("[main] Constructing");
MyDisposable m = new MyDisposable(0);
MyMethod(1);
Console.WriteLine("[main] Disposing [object 0]");
m.Dispose();
Console.WriteLine("[main] GC Collecting");
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("[main] Done");
Console.ReadKey();
}
private static void MyMethod(int i)
{
new MyDisposable(i);
}
}
public class MyDisposable : IDisposable
{
private int _id;
public MyDisposable(int id)
{
_id = id;
Console.WriteLine($"[object {_id}] Constructed");
}
private bool disposed = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
Console.WriteLine($"[object {_id}] Disposing by Dispose()");
}
else
{
Console.WriteLine($"[object {_id}] Disposing by ~Finalizer");
}
Console.WriteLine($"[object {_id}] Disposed");
disposed = true;
}
else
Console.WriteLine($"[object {_id}] Already disposed!");
}
~MyDisposable()
{
Dispose(false);
}
}
Output
[main] Constructing
[object 0] Constructed
[object 1] Constructed
[main] Disposing [object 0]
[object 0] Disposing by Dispose()
[object 0] Disposed
[main] GC Collecting
[object 1] Disposing by ~Finalizer
[object 1] Disposed
[main] Done
Some read: Using objects that implement IDisposable.
The official Cleaning up unmanaged resources
states:
If your types use unmanaged resources, you should do the following:
Implement the dispose pattern. (...)
In the event that a consumer of your type forgets to call Dispose, provide a way for your unmanaged resources to be released. There are two ways to do this:
Use a safe handle to wrap your unmanaged resource. This is the recommended technique. Safe handles are derived from the System.Runtime.InteropServices.SafeHandle abstract class and include a robust Finalize method. When you use a safe handle, you simply implement the IDisposable interface and call your safe handle's Dispose method in your IDisposable.Dispose implementation. The safe handle's finalizer is called automatically by the garbage collector if its Dispose method is not called.
(...)
Implement a Dispose method
contains:
'the general pattern for implementing the dispose pattern for a base class that uses a safe handle'; and
'the general pattern for implementing the dispose pattern for a base class that overrides Object.Finalize'.

Dispose/finalize pattern : disposing managed ressources

Let's imagine I have a class named Base with 3 attributes :
class Base : IDisposable
{
private string _String;
private Class1 classe1;
private int foo;
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
public virtual void Dispose(bool disposing)
{
if (disposing)
{
Console.WriteLine("Free Managed ressources");
//HOW TO FREE _String, class1 and foo ?!
}
Console.WriteLine("Free unmanaged ressources");
}
~Base()
{
this.Dispose(false);
}
}
and a classe named Class1 with 2 attributes :
class Class1
{
public int entier { get; set; }
public string Nom { get; set; }
}
My question is : How can I free the attributes of Base in the Dispose method ? (_String, classe1, foo)
My question is : How can I free the attributes of Base in the Dispose
method ? (_String, classe1, foo)
You don't need to, that's the job of the garbage collector. Implementing IDisposable is a way for the framework to let you release any unmanaged resources you have allocated, and dispose managed objects implementing IDisposable themselves (which in turn hold other unmanaged resources).
None of the managed objects at your disposable implement IDisposable, and they will be collected once there is no longer any objects pointing to your Base class. When will that happen? In an arbitrary time, when the GC see's that there is no longer space in generation 0, and it needs to collect. There is nothing you need to do.
Implementing IDisposable does not mean "this object will be collected immediatly once i run Dispose()", it merely means that the framework gives you a chance to reclaim any resources it might not be aware of (such as unmanaged ones). It is a recommended approach, if one implements a finalizer, to suppress the call to it via GC.SuppressFinalize, saving the GC the trouble of moving your object from the Finalizer Queue to the F-Reachable Queue, hence making it available for collection earlier.
when will these 3 attributes free from the heap ? The garbage
collector won't free them because I have GC.SuppressFinalize(this)
You have a basic misunderstanding of how the GC works and what SuppressFinalize means. The GC will run at an non-deterministic time, and you basically shouldn't care when that happens. It's his responsibility to clean up after you. Calling SuppressFinalize on an object implementing a finalizer does nothing more than set a bit in the objects header which the runtime checks when calling finalizers, which will suppress your finalizer from running
In this case, you shouldn't implement IDisposable at all, or if it was there because it was deemed very likely that it could be necessary in the future, then it would have an empty implementation. You certainly shouldn't have a finaliser in there; never have one unless you actually need one with 100% certainty.
There are a few cases where you would want to implement IDisposable, and in some of those cases you'd also want to have a destructor (which is the C# way of having a finaliser).
One is where you have something that it is really important to do when the object is finished with, most often undoing something you have previously done, such as releasing a handle that you'd obtained, closing a connection you'd opened, etc. but not managed memory. (All objects use managed memory, and all objects have their managed memory cleaned up for them if they're can't be used again and more managed memory is needed by something else, that's what the managed in "managed memory" means).
public class SomeClass : IDisposable
{
private IntPtr _someHandle;
public SomeClass(string someIdentifier)
{
_someHandle = GetHandle(someIdentifier);
}
public void Dispose()
{
ReleaseHandle(_someHandle);
}
}
So now whenever something that's been using a SomeClass is done with it, it calls Dispose() on it (perhaps implicitly via a using block) and all is cleaned up nicely.
But what if that doesn't happen? Well, that's why we might have a finaliser:
public class SomeClass : IDisposable
{
private IntPtr _someHandle;
public SomeClass(string someIdentifier)
{
_someHandle = GetHandle(someIdentifier);
}
public void Dispose()
{
ReleaseHandle(_someHandle);
_someHandle = null; // so we know not to release twice.
}
~SomeClass()
{
if(_someHandle != null)
ReleaseHandle(_someHandle);
}
}
So, here if the Dispose() doesn't get called, we still get the clean-up, because the normal garbage-collection process:
Realise you need more memory.
Find objects that aren't going to be used any more.
Reclaim the memory of those objects.
Has the following steps added:
Realise the object whose memory you were going to reclaim has a finaliser to run.
Put the object into a queue of other such objects.
(On a separate thread) run the finaliser of the object.
The object is no longer an object that "has a finaliser to run" as per step 4 above, so next time around it can be reclaimed.
All of this has downsides:
We can't guarantee when, if ever, this will happen.
We didn't get to reclaim as much memory in step 3, because there was such an object.
Garbage collection is generational, and playing nicely with generational collection for an object means either dying quickly or living a long time, dying just after the first time the GC tried to collect an object is pretty much the least optimal time.
We can get around the first two by calling Dispose() rather than letting finalisation happen, which is up to the user of the class, not the class itself. We get around the third by having an object that knows it doesn't need to be finalised mark itself as no longer needing to be:
public class SomeClass : IDisposable
{
private IntPtr _someHandle;
public SomeClass(string someIdentifier)
{
_someHandle = GetHandle(someIdentifier);
}
public void Dispose()
{
ReleaseHandle(_someHandle);
GC.SuppressFinalize(this);
}
~SomeClass()
{
ReleaseHandle(_someHandle);
}
}
If an object has been passed to GC.SuppressFinalize() then step 4 and subsequent don't happen.
The second case where you might what to implement IDisposable is where you have an IDisposable object as a field of another object that "owns" it (controls it's lifetime):
public class SomeOtherClass : IDisposable
{
private SomeClass _someObj;
public SomeOtherClass(string someIdentifier)
{
_someObj = new SomeClass(someIdentifier);
}
public void Dispose()
{
//If base type is disposable
//call `base.Dispose()` here too.
_someObj.Dispose();
}
}
Cleaning up a SomeOtherClass hence means cleaning up the SomeClass it has as a field. Note that here we do not have a finaliser here. We can't need a finaliser, because it would have nothing to do; at best it would do nothing and just have the downsides of finalisers mentioned above, at worse it would try to clean up _someObj without knowing whether this would happen before or after _someObj cleaning itself up and with _someObj queued to clean itself up in a way where it can assume nothing else will do the clean-up.
For the third case, consider if we combine the two cases with a class that has both an unmanaged resource it releases and a field which is a disposable class. Here if we are Dispose()d we want to clean up both, but if we are finalised we want to only clean up the unmanaged resource that is dealt with directly:
public sealed class SomeHybridClass : IDisposable
{
private IntPtr _someHandle;
private SomeClass _someObj;
public SomeHybridClass(string someIdentifier)
{
_someHandle = GetHandle(someIdentifier);
_someObj = new SomeClass(someIdentifier);
}
public void Dispose()
{
ReleaseHandle(_someHandle);
GC.SuppressFinalize(this);
_someObj.Dispose();
}
~SomeHybridClass()
{
ReleaseHandle(_someHandle);
}
}
Now, since there's repetition here, it makes sense to refactor them into the same method:
public sealed class SomeHybridClass : IDisposable
{
private IntPtr _someHandle;
private SomeClass _someObj;
public SomeHybridClass(string someIdentifier)
{
_someHandle = GetHandle(someIdentifier);
_someObj = new SomeClass(someIdentifier);
}
private void Dispose(bool disposing)
{
if(disposing)
{
_someObj.Dispose();
}
ReleaseHandle(_someHandle);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~SomeHybridClass()
{
Dispose(false);
}
}
And for a fourth case, imagine if this class wasn't sealed; it's derived types also need to be able to do this clean-up, so we make the parameterised Dispose(bool) method protected:
public class SomeHybridClass : IDisposable
{
private IntPtr _someHandle;
private SomeClass _someObj;
public SomeHybridClass(string someIdentifier)
{
_someHandle = GetHandle(someIdentifier);
_someObj = new SomeClass(someIdentifier);
}
protected virtual void Dispose(bool disposing)
{
// if this in turn was derived, we'd call
// base.Dispose(disposing) here too.
if(disposing)
{
_someObj.Dispose();
}
ReleaseHandle(_someHandle);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~SomeHybridClass()
{
Dispose(false);
}
}
However, these last two examples are really solving the wrong problems: They're solving the problem of how to have a class that has both a disposable type as a field and an unmanaged resource, and/or be part of a type hierarchy with this happening. Really you're much better off never getting into this situation; either have a class that only deals with an unmanaged resource (and is sealed) or has disposable types in fields, and you end up with only having the deal with the first two cases. If you deal with your unmanaged resources by deriving from SafeHandle then you are really only having to worry about the second case, and that also manages some difficult edge cases too.
Really, finalisers should very, very rarely be written, and when they are written they should be written to be as simple as possible, because there's enough complication inherent to them and the edge-cases around them as it is. You need to know how to deal with overriding protected virtual void Dispose(bool disposing) (note, should never be public) to deal with the legacy of when that had seemed like a good idea to someone, but not have inheritable classes with both unmanaged and managed-disposable resources forcing someone else into that position.
How can I free the attributes of Base in the Dispose method ? (_String, classe1, foo)
As should now be clear, those fields (attributes are a very different thing in .NET) don't need to be freed. The only resource they have is managed memory, so once they can't be reached (aren't in a static, aren't about to have something done to them in a method, and aren't in a field of something that is in either of those categories or a field of something that is in a field in either of those, etc.) their memory will be automatically reclaimed when needed.

Garbage collection methods in c# application

I have this question :
I can't understand why the second choice is the answer . I mean other methods ( KeepAlive and CancelFullGCNotification) will prevent system to call the finalizer.
What are the differences between the four methods?
In which cases, we have to use it?
The KeepAlive will only delay the finalizer being called on a class (by making the object live longer and not having it be eligible for finalization) and CancelFullGCNotification has nothing to do with finalizing.
Only SuppressFinialize will prevent the finalizer from running on a class.
The use of SuppressFinalizer is only necessary when you have coded a finalizer. Most of the time you would not need to suppress. Anyway, B is correct answer and the only way to prevent the destructor/finalize method from being called.
More importantly... the finalizer should rarely ever be used and delays garbage collection by itself.
It's there to clean up unmanaged resources and this is the pattern typically used:
public class SomeClass : IDisposable
{
private bool disposed;
//disposing is true if you're disposing managed resources
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
//Dispose managed resources
}
//Dispose unmanaged resources
disposed = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~SomeClass()
{
Dispose(false);
}
}

Writing the correct IDisposable implementation for classes with COM objects

I have a class that uses a .NET wrapper for a COM object and well it is giving me that infamous RCW error, so in my investigation I found that if I take out the Dispose() method from the finalizer of this class it will fix the RCW error so something is wrong for example the object is getting disposed but the registered events are still hanging around ... But just removing the Dispose() can't be the answer because then who is going to release the memory ? ( I ran a Memory profiler and confirmed that just removing the Dispose method causes like 20MB of extra Unmanaged Memory )
so something should be wrong with the way I am using the Dispose model.. here is what I have:
private MyCOMobject theCOMobject = null;
static SuppressFieldCntrlr()
{
new SomeCalss();
}
~SuppressFieldCntrlr()
{
Dispose(false);
}
private bool disposed = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
theCOMobject.Dispose();
}
MethodFoo(false);
disposed = true;
}
}
For COM objects, you'll want to call Marshal.ReleaseComObject. The Marshal class is found in the namespace System.Runtime.InteropServices.
More info here.
Unmanaged resources should be disposed outside of the if(disposing) part. In there only managed resources should be disposed.
In the COM wrapper:
private void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
//Dispose managed resource if any.
}
//Release the unmanaged resource.
disposed = true;
}
}
and keep your implementation of the Dispose Pattern as is since the COM wrapper implements IDisposable.
I'm not sure about ReleaseComObject. It is not allways recommended apparently. There is also the Marshal.FinalReleaseComObject method. Also have a look at this answer on how to wrap a COM object.
For detailed guidelines on how to implement the dispose pattern you can read this blog entry by Joe Duffy. It is rather long but very helpful.

Reason for Using IDisposable Interface

We all know that IDisposable interface is using for disposing unmanaged resources.
I have a class which contains following code. here i have implemented the Dispose method from IDisposable interface.
class ClassA:IDisposable
{
public ClassA()
{
Console.WriteLine("ClassBeingTested: Constructor");
}
private bool disposed = false;
Image img = null;
public Image Image
{
get { return img; }
}
~ClassA()
{
Console.WriteLine("ClassBeingTested: Destructor");
// call Dispose with false. Since we're in the
// destructor call, the managed resources will be
// disposed of anyways.
Dispose(false);
}
public void Dispose()
{
Console.WriteLine("ClassBeingTested: Dispose");
// dispose of the managed and unmanaged resources
Dispose(true);
// tell the GC that the Finalize process no longer needs
// to be run for this object.
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposeManagedResources)
{
// process only if mananged and unmanaged resources have
// not been disposed of.
if (!this.disposed)
{
Console.WriteLine("ClassBeingTested: Resources not disposed");
if (disposeManagedResources)
{
Console.WriteLine("ClassBeingTested: Disposing managed resources");
// dispose managed resources
if (img != null)
{
img.Dispose();
img = null;
}
}
// dispose unmanaged resources
Console.WriteLine("ClassBeingTested: Disposing unmanaged resouces");
disposed = true;
}
else
{
Console.WriteLine("ClassBeingTested: Resources already disposed");
}
}
// loading an image
public void LoadImage(string file)
{
Console.WriteLine("ClassBeingTested: LoadImage");
img = Image.FromFile(file);
}
}
What my doubt is why i need to implement the Dispose method from IDisposable interface?. Instead of that i can create my own Dispose method in my class without inheriting from IDisposable interface which i have given below.
for the class below i haven't inherited my class from IDisposable interface. instead of that i created my own dispose method. this also works fine.
class ClassA
{
public ClassA()
{
Console.WriteLine("ClassBeingTested: Constructor");
}
private bool disposed = false;
Image img = null;
public Image Image
{
get { return img; }
}
~ClassA()
{
Console.WriteLine("ClassBeingTested: Destructor");
// call Dispose with false. Since we're in the
// destructor call, the managed resources will be
// disposed of anyways.
Dispose(false);
}
public void Dispose()
{
Console.WriteLine("ClassBeingTested: Dispose");
// dispose of the managed and unmanaged resources
Dispose(true);
// tell the GC that the Finalize process no longer needs
// to be run for this object.
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposeManagedResources)
{
// process only if mananged and unmanaged resources have
// not been disposed of.
if (!this.disposed)
{
Console.WriteLine("ClassBeingTested: Resources not disposed");
if (disposeManagedResources)
{
Console.WriteLine("ClassBeingTested: Disposing managed resources");
// dispose managed resources
if (img != null)
{
img.Dispose();
img = null;
}
}
// dispose unmanaged resources
Console.WriteLine("ClassBeingTested: Disposing unmanaged resouces");
disposed = true;
}
else
{
Console.WriteLine("ClassBeingTested: Resources already disposed");
}
}
// loading an image
public void LoadImage(string file)
{
Console.WriteLine("ClassBeingTested: LoadImage");
img = Image.FromFile(file);
}
}
So can any one tel me tat the reason behind of implementing dispose method from IDisposable interface.
When you implement IDisposable, you can use using for exception-safe disposing.
Two reasons:
Implementing IDisposable expresses intent (this should be disposed) in a conventional manner. It's easy to spot that you should dispose of a resource if it implements IDisposable, whereas relying on other developers to read the documentation carefully is more likely to lead to errors.
That convention is used by the using statement. You should be able to write:
using (ClassA foo = new ClassA())
{
...
}
... but unless you implement IDisposable, you can't.
(Personally I wouldn't include a finalizer, by the way... that's usually only for directly held unmanaged resources; here you should rely on the Image finalizer.)
Although implementing your own dispose method will suffice for any explicit call made to that method but when,after implementig Idispobale, you use a using statement, the built in .Net functionality will invoke your dispose method for you so your code can look like this;
using (ClassA foo = new ClassA())
{
...
}
rather than this
using (ClassA foo = new ClassA())
{
....
foo.Dispose();
}
When you implement IDisposable interface you can use your class in using block, like this:
...
using(var classA = new ClassA())
{
do some stuff
}
after it your Dispose method will be invoked.
It is quite useful.
IDisosable is very useful. For example working with Database connections. It requires all of its implementations to have Dispose method.
It is very useful for working with unmanaged resources, where you have to release them as for the Database connections above, because Garbage Collector is working on managed code (it releases objects where they are not in use anymore), but for unmanaged as C++ application or library (without CLR), it has no idea what is going there. By using its disposable method you say you have finished your work with such a resource and it is being released (freeing memory).
Example (example class and usage):
class Database : IDisposable
{
private SqlConnection _conn;
public Database(string conn_str)
{
// Doing the connectivity
}
public Dataset Read(string sql)
{
SqlCommand cmd = new SqlCommand();
Dataset result = null;
// etc...
return result;
}
public Dispose()
{
_conn.Close();
}
}
using(Database db = new Database(conn_string))
{
Dataset ds = db.Read("SELECT something FROM table");
}
The this approach is very useful. In this statement you init your IDisposable and at the end of statement (not needed to call db.Dispose()) its Dispose() is called and the resources are freed. In this case unmanaged resource may be the .NET SQL Driver, you can use MySQL library, etc.
Three "unwritten" parts of the contract for .net objects are:
If a class implements IDisposable, then once the instance is no longer needed, calling IDisposable.Dispose() on it and abandoning it should take care of all necessary cleanup, regardless of the type of object.
If an object does not implement IDisposable, it may be safely abandoned any time it is no longer needed, without requiring any cleanup.
An object which constructs an instance of a class which implements IDisposable is responsible for ensuring that IDisposable gets called, either by doing it itself when it is done with the object, or ensuring that the reference gets handed off to some other object which will carry out the responsibility.
This is in general a very useful pattern. While there are some rare occasions when it will be impossible for a class to properly implement IDisposable (most commonly because the class exists to expose a complex resource whose cleanup semantics are understood by the user of the class, but not by the class itself), it's very useful to have a unified means of cleanup when things like exceptions occur.

Categories

Resources