Calling C++/CLI delete on C# object - c#

I'm in the middle of converting some code from C++/CLI to C#. One of the objects has a destructor in the C++/CLI version. Some other C++/CLI code calls "delete" on this object after use.
Which method do I need to implement in the C# version of this object so those "delete"s continue to function the same (IDisposable.Dispose, the finalizer, or something else that I'm missing)?

I would say the IDisposable interface is what you look for if you need deterministic disposal of resources. This is usually the case with unmanaged resources, such as unmanaged handles that need to be closed, streams or database connections.
In C++/CLI, if you declare a managed type (ref class and the like), IDisposable is implemented using the destructor syntax, and Dispose() is called by using the delete keyword. If you declare such an object of a managed type locally (without using the ^ operator or gcnew), C++/CLI even automatically calls Dispose() for you when the object goes out of scope. In that way, C++/CLI is more convenient than C#.
You won't be able to call delete on the object when using C#, you'll need to call Dispose() manually on it instead. Another way to dispose of IDisposable objects is the using block.
The finalizer (implemented in C# by using destructor syntax) is not the same as a C++ destructor, since it is not deterministic when it will be called. Objects with a finalizer are basically queued until the finalizer thread decides to call their finalizer, so effectively you never know exactly when that is called.
The best approach for dealing with unmanaged resources is probably a combination of the two. See here for the recommended approach:
http://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.100).aspx
Note, however that when using IDisposable, even though you can dispose of unmanaged resources deterministically, managed objects still need to be collected by the garbage collector (non-deterministically).
I just found an article explaining the differences of this between C++/CLI and C#. You might find it interesting:
http://weblogs.thinktecture.com/cnagel/2006/04/ccli-finalize-and-dispose.html

There's a terminology mismatch between C++/CLI and C#. The C++/CLI destructor (~Foo) is the IDisposable.Dispose() method implementation. A C++/CLI class doesn't explicitly implement IDisposable, it is automatic from just the presence of a destructor.
The C++/CLI finalizer (!Foo) is the C# destructor.
So the C++/CLI delete operator is equivalent to calling the Dispose() method. Beware of stack semantics in C++/CLI, triggered by a local variable of a reference type without the ^ hat. The compiler generates a hidden call to Dispose() at the end of the scope block. Which is equivalent to the C# using keyword. Hard to see from the source code so pay extra attention or look at the generated IL with ildasm.exe

C# doesn't provide the same tools, but it does provide patterns for you:
If your dtor is closing a stream, unlinking pointers, or setting states appropriately then I feel the IDisposable interface is a good fit:
// C#
void Dispose()
{
_stream.Close();
_ptr = null;
_running = false;
}
// with
obj.Dispose();
You can't really force GC to keep memory open. There are ways to help the GC know what can and should be freed, Read http://msdn.microsoft.com/en-us/library/ee787088.aspx for more info.
Keep in mind that using IDisposable expects you to call the Dispose() method appropriately. So in place of each delete, you would want to use Dispose(). The safe part of a finalizer is that when the GC is actually freeing it, it'll be called. However, you can't call the finalizer yourself (so if timing on delete/dispose is important, that isn't the right solution.)

To me, it really depends on what the destructor is doing. If it's doing something like releasing an unmanaged resource (SQL or file connections, for example), then I would implement IDispose and close the connection in the Dispose() method.
If it's destroying other objects that do not require any explicit cleanup, then I would just leave the destructor out and let the GC handle it.

Related

Dispose(true) never called on Xamarin.Android 'Java.Object' [duplicate]

I am confused about dispose. I am trying to get my code disposing resources correctly. So I have been setting up my classes as IDisposable (with a Dispose method) them making sure that the Dispose method gets called.
But now FXCop is telling me lots of stuff about disposing = false and calling Dispose(false).
I don't see a Dispose method that takes a bool. Do I need to make one? If so, why? Why not just have a method that gets called when it is disposing?
I saw some code here: CA1063: Implement IDisposable correctly - Microsoft Docs that shows how to make a Dispose method that takes a bool. It says it is for native vs managed resourses. But I thought the whole point of dispose was for unmanaged resourses only.
Also, the line that FXCop is complaining about is this:
~OwnerDrawnPanel()
{
_font.Dispose();
}
It says:
CA1063 : Microsoft.Design : Modify 'OwnerDrawnPanel.~OwnerDrawnPanel()' so that it calls Dispose(false) and then returns.
But Font does not have a Dispose(bool) on it (that I can find).
To sum it up:
Why do I need a Dispose(bool)? and if I do, why doesn't Font have it? and since it does not have it, why is FXCop asking me to use it?
Thanks for all the great answers. I think I understand now. Here is
The answer as I see it:
Disposing of "unmanaged" resources falls into two categories:
Resources that are wrapped in a managed class (ie Bitmap, Font etc), but still need Dispose to be called to clean them up properly.
Resources that you have allocated, which are representations of native resources (ie device contexts that need to be released)
Dispose(bool) is used to tell the difference between the two:
When Dispose is directly called on your object, you want to free both kinds of "unmanaged" resources.
When your object is up for Garbage Collection, you don't need to worry about the first kind of resources. The garbage collector will take care of them when it cleans them up. You only need to worry about true native resources that you have allocated (if any).
Dispose(bool) is a pattern to implement Finalize and Dispose to Clean Up Unmanaged Resources , see this for detail
IDisposable provides a method with the signature
public void Dispose()
Microsoft best practices (Implement a Dispose method) recommend making a second private method with the signature
private void Dispose(bool)
Your public Dispose method and finalizer should call this private Dispose method to prevent disposing managed resources multiple times.
You can fix the warning you are getting by either implementing IDisposable and disposing of your font object in the dispose method, or creating a Dispose(bool) method in your class, and make your finalizer call that method.
Dispose(bool) is not meant to be public and that is why you don't see it on Font.
In case some user of your class forgets to call Dispose on your method, you will release the unmanaged resources only by making a call to Dispose(false) in the Finalizer.
In case IDispose is called correctly, you call the Dispose on managed resources and also take care of the unmanaged.
The flag is to distinguish the two cases.
It is a pattern recommended by MSDN.
FxCop says you should implement the Disposable pattern like described here. Note that you should not use finalizers for disposing managed resources like _font is. Finalizers are used for cleaning up unmanaged resources. If you do not execute the cleanup logic in the Dispose method of your (sub)class they are executed non-deterministically by the garbage collector.
Also please note that it is very rarely that you need to do anything in the destructor. Regularly, everything is taken care of by the garbage collector. For example, in your code, you don't need to dispose the _font object in the OwnerDrawnPanel's destructor. Since the panel is getting cleaned up by the GC, so will be _font, because panel was the only one who referenced it, right?
Generally, if you own disposable objects, you only need to dispose them when your own Dispose method was called. But NOT in the destructor. When your destructor is running, you can bet that all your aggregated objects are being cleaned up as well.
You should almost never need to use finalizers. They are only for classes that directly contain unmanaged resources, and in .NET 2.0+ those should be wrapped in SafeHandle.
I think Dispose(true) will free both the managed and unmanaged resource as we need to not call finalize again that's why we write GC.SupressFinalize() after Dispose(true).
We call Dispose(false) in destructors to free unmanaged resources and will be called by Runtime and not user's code.
Agreeing with Kumar, the Dispose(bool disposing) pattern is also documented on MSDN. The distinction is not between managed and unmanaged resources, but whether Dispose is being called by your code or the runtime.
I found a good article about correct implementation of IDispose interface: http://msdn.microsoft.com/en-us/library/ms244737(v=vs.80).aspx
The pattern of implementing a public public void Dispose(), protected virtual void Dispose(bool), and ~ClassName() finalizers is a best practice recommended by Microsoft as a way to neatly organize your cleanup code for both managed and unmanaged resources.
Basically, the code that uses your Disposable class should call Dispose(), but if it doesn't, the finalizer ~ClassName() will get called by Garbage Collection, and based on which one of those is used, you set the argument to Dispose(bool) as true or false, and in your Dispose(bool), you only clean up managed resources if the argument is true.
The warning you are getting seems to specifically recommend that you use this practice in your finalize method ~ClassName().

How GC releases managed resources in C#

As of my knowledge i know like GC performs collection operation in order to remove unmanaged resources to release memory which is called implicit clean up. And by using 'USING' keyword we can do it explicit cleaning but my doubt is how GC releases managed resources.
You don't have to do anything special in order for GC to clean up your managed resources. GC will clean it up sometime after there are no references left to your managed resource.
If your managed resource owns unmanaged resources, you can implement the IDisposable interface and call the Dispose method, in which you'd explicitly clean up your unmanaged resources. using statement makes it very easy to use this interface, as it automatically calls Dispose when the code exists the using block, even in case of exceptions.
You might take a look at the MSDN documentation on Garbage Collection.
EDIT: based on comments above.
You could override Object.Finalize by defining a finalizer (e.g. ~MyClass()) but there is no telling when it will be called by GC. IDisposable is generally preferred. More info on Finalizer vs Dispose here.

When should I create a destructor?

For example:
public class Person
{
public Person()
{
}
~Person()
{
}
}
When should I manually create a destructor?
When have you needed to create a destructor?
UPDATE: This question was the subject of my blog in May of 2015. Thanks for the great question! See the blog for a long list of falsehoods that people commonly believe about finalization.
When should I manually create a destructor?
Almost never.
Typically one only creates a destructor when your class is holding on to some expensive unmanaged resource that must be cleaned up when the object goes away. It is better to use the disposable pattern to ensure that the resource is cleaned up. A destructor is then essentially an assurance that if the consumer of your object forgets to dispose it, the resource still gets cleaned up eventually. (Maybe.)
If you make a destructor be extremely careful and understand how the garbage collector works. Destructors are really weird:
They don't run on your thread; they run on their own thread. Don't cause deadlocks!
An unhandled exception thrown from a destructor is bad news. It's on its own thread; who is going to catch it?
A destructor may be called on an object after the constructor starts but before the constructor finishes. A properly written destructor will not rely on invariants established in the constructor.
A destructor can "resurrect" an object, making a dead object alive again. That's really weird. Don't do it.
A destructor might never run; you can't rely on the object ever being scheduled for finalization. It probably will be, but that's not a guarantee.
Almost nothing that is normally true is true in a destructor. Be really, really careful. Writing a correct destructor is very difficult.
When have you needed to create a destructor?
When testing the part of the compiler that handles destructors. I've never needed to do so in production code. I seldom write objects that manipulate unmanaged resources.
It's called a "finalizer", and you should usually only create one for a class whose state (i.e.: fields) include unmanaged resources (i.e.: pointers to handles retrieved via p/invoke calls). However, in .NET 2.0 and later, there's actually a better way to deal with clean-up of unmanaged resources: SafeHandle. Given this, you should pretty much never need to write a finalizer again.
You don't need one unless your class maintains unmanaged resources like Windows file handles.
It's called a destructor/finalizer, and is usually created when implementing the Disposed pattern.
It's a fallback solution when the user of your class forgets to call Dispose, to make sure that (eventually) your resources gets released, but you do not have any guarantee as to when the destructor is called.
In this Stack Overflow question, the accepted answer correctly shows how to implement the dispose pattern. This is only needed if your class contain any unhandeled resources that the garbage collector does not manage to clean up itself.
A good practice is to not implement a finalizer without also giving the user of the class the possibility to manually Disposing the object to free the resources right away.
I have used a destructor (for debug purposes only) to see if an object was being purged from memory in the scope of a WPF application. I was unsure if garbage collection was truly purging the object from memory, and this was a good way to verify.
When you have unmanaged resources and you need to make sure they will be cleaned up when your object goes away. Good example would be COM objects or File Handlers.
Destructors provide an implicit way of freeing unmanaged resources encapsulated in your class, they get called when the GC gets around to it and they implicitly call the Finalize method of the base class. If you're using a lot of unmanaged resources it is better to provide an explicit way of freeing those resources via the IDisposable interface. See the C# programming guide: http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx
Answer:
When you have to release unmanaged resources, like file handles, database connections, etc.
// Example
public class Person
{
private FileStream _fileStream;
public Person()
{
_fileStream = new FileStream("test.txt", FileMode.Open);
}
~Person()
{
_fileStream.Close();
}
}
Explanation:
The destructor is called when the object is garbage collected. It is not called when the object is destroyed by the programmer. It is called when the object is no longer referenced by any other object. It is called when the object is no longer in scope. It is called when the object is no longer in memory.

What are the kind of variables that must be disposed? (.NET/Java)

Three questions:
What kind of variables should be disposed manually in .NET/Java? I know that SqlConnection should always be either disposed manually or used in a using{} block. Is it right? What are the other kind of variables that should be disposed?
I read somewhere that unmanaged code must be disposed manually. Is that right? What exactly is unmanaged code and how do I know if a variable is managed or unmanaged?
Finally, how do I dispose variables? I know that the Dispose() method does not really dispose a variable. So what does Dispose() do? Should I set them to null()? What is the logic by which the garbage collector works?
This answer deals only with the .NET part of your question
What kind of variables should be disposed manually in .NET/Java? I know
that SqlConnection should always be
either disposed manually or used in a
using{} block. Is it right? What are
the other kind of variables that
should be disposed?
In .NET, all objects that implement IDisposable should be disposed explicitly (or used in a using block).
I read somewhere that unmanaged code must be disposed manually. Is
that right? What exactly is unmanaged
code and how do I know if a variable
is managed or unmanaged?
You probably mean unmanaged resources, as code can't be disposed... All classes that use unmanaged resources (memory allocated not on the managed heap, win32 handles...) should implement IDisposable, and should be disposed explictly, since they are not managed by the garbage collector.
Finally, how do I dispose variables? I know that the Dispose()
method does not really dispose a
variable. So what does Dispose() do?
Should I set them to null()? What is
the logic by which the garbage
collector works?
I'm not sure I understand your question... you don't dispose a variable, it is managed by the garbage collector. All managed memory is automatically released when it's not used anymore (i.e. it is not accessible by code because there are no references left to it). The IDisposable.Dispose method is only intended for resources that are not managed by the GC.
EDIT: as a side note, I would like to add that IDisposable is primarily intended to clean-up unmanaged resources, but is also frequently used to perform other cleanup actions and guarantee state or data integrity. For instance, IDbTransaction implements IDisposable in order to rollback the transaction if an exception occurs before the transaction was committed :
using (var trx = connection.BeginTransaction())
{
// Do some work in the transaction
...
// Commit
trx.Commit();
} // the transaction is implicitly rolled back when Dispose is called
In Java, you "close" rather than "dispose".
JDBC Connections, unless you got them from a pool.
JDBC ResultSets, depending on the JDBC connector.
InputStreams, OutputStreams, Readers and Writers (with the exception of those that are byte array and string backed).
Some third-party Java libraries or frameworks have classes that need to be manually disposed / closed / destroyed.
.NET
All objects that implement the IDisposable interface are asking that their Dispose methods are called when they are no longer required. The using block is just C# sugar for a try-finally block that disposes the IDiposable in the finally block, as long as it is not null. Consequently, disposal occurs even if an exception is thrown in the try block.
Unmanaged Code.
The Dispose method does whatever code the developer who wrote the Dispose method put in it! Typically this involves releasing 'unmanaged' resources, which are not managed by the runtime, such as database connections, window handles and file handles. Note that the GC manages the heap, so setting a reference to null just before the object it references becomes unreachable doesn't help all that much.
This is a good read.
This was mostly covered by Thomas, but to expand on the third point:
Finally, how do I dispose variables? I know that the Dispose()
method does not really dispose a
variable. So what does Dispose() do?
Should I set them to null()? What is
the logic by which the garbage
collector works?
They key is that the Dispose() tells the object to free any unmanaged resources that it currently holds, it does not free the object itself.
The garbage collector knows how to free the object, but for IDisposable objects, only the object itself knows how to dispose of it's private resources, so you have to make sure that Dispose() gets called before the garbage collector frees the object.
For Java:
SWT is a framework where we have to dispose resources (like images), because the framework uses native libs and system handles which have to released. SWT classes are documented and tell, when dispose is required.
Following up on other .NET solutions...
If your object owns unmanaged resources, it's not enough to simply clean them up on Dispose() calls, because you have no guarantee that Dispose will get called. The full solution is:
Add a finalizer for the class.
Add a public bool property called Disposed, which indicates that the object has been disposed.
Add a protected Dispose(bool) method. The public Dispose() method has no parameters.
When Dispose() is called, if the Disposed property is false, it will call Dispose(true), and call GC.SuppressFinalize() to disable the finalizer. Ths is essential to keep .NET's garbage collector happy. Classes with unspressed finalizers go to the end of the line for garbage collector cleanup, so they become almost a memory leak.
The public Dispose() method should then set the Disposed property to true.
The finalizer calls Dispose(false).
The Dispose(bool) will always clean up its own managed resources, but it will clean up unmanaged resources only when called as Dispose(true). Note that all resource cleanup is in the protected Dispose(bool) method, not in the public Dispose() method.
If the class is not sealed, then the protected Dispose(bool) method should be a virtual method. Subclasses that need to do cleanup can override Dispose(bool) and followht e same logic as above. In addition, they should call base.Dispose(bool) with the parameter they were given.
If .net, if you can use an object in a "using" statement, that means the object implements iDisposable and you should when practical call it's disposal method when you're done with it. Note that the disposal method may not always be called "Dispose", but you can always call it by casting the object to iDisposable and then calling Dispose on that.
Note that some types of "iDisposable" objects don't handle any unmanaged resources, but do subscribe to events. If such an object isn't properly disposed, it might not get garbage-collected unless or until all of the objects for which it holds subscribed events are themselves garbage-collected. In some cases, that may never happen until an application exits.
For example, the enumerator returned by an iEnumerable collection might subscribe to that object's "collection changed" event, unsubscribing when its dispose method is called. If the dispose method is never called, the enumerator would remain around as long as the collection did. If the collection itself stayed around a long time and was enumerated frequently, this could create a huge memory leak.
I'm in agreement with the above, I'll just add to the subsequent question about setting a variable to null.
You don't need to do this with variables used in a method (they will fall out of scope so it's only if they have state within them that must be signalled as needing clean-up, through IDisposable.Disopse that you have to worry abou this).
It's rarely useful for instance or static members, as memory isn't as precious a resource as people often think (it is a precious resource, but most attempts to deal with it in a few lines of code is like turning off your taps while you've a burst main.
It is though useful if you have a static or instance member of a class and (A) it is a large object (B) the "owning" object is likely to stay in memory for a long time and (C) you know you won't need that value gain, to set that member to null.
In practice this isn't a very common combination. If in doubt, just leave alone.
Now, do read the other answers here, as what they say about the Dispose() method is much more important.

Do I have to release all the native resource before a GC happens?

We know that .NET has 2 super types. The Value-Type and Reference-Type. As I understand it, after I set all the roots of an Ref-Type object to null, the Ref-Type object is logically deleted from the stack, but remains in existence on the manged-heap. If the Ref-Type object uses some native resources, such as an opened-file, do I have to release these resources before or after I setting all the roots to null? And why?
If a reference type has native resources, whether directly or indirectly, it should implement IDisposable. You should call Dispose when you've finished using the object (assuming you "own" the resource), preferably with a using statement:
using (Foo foo = new Foo(...))
{
...
} // Dispose called here
The Dispose method should release all the resources.
If the type has direct references to unmanaged resources - usually via an IntPtr - then it should have a finalizer. However, .NET 2.0 made this a lot simpler with SafeHandle which can be used instead of IntPtr. The advent of SafeHandle basically means that very very few types should have a finalizer these days.
There are a few things that need to be clarified in your assumptions in order to give you an answer.
For most intents and purposes, .NET only has one "super" type, and that is System.Object.
(For the record there are some types in the type system that do not derive from System.Object)
System.ValueType inherits from System.Object.
Once you release all of the references to an object, then the object becomes eligible for garbage collection. It does not get deleted when the last reference is released. At some indeterminate point in the future, the memory that the object was stored in on the managed heap will be reclaimed by a garbage collection.
If the instance does in fact use some sort of native resources, then it should implement the IDisposable interface.
If this interface is implemented on the class your object is an instance of, then you should call the Dispose method, as a "proper" implementation of IDisposable would release any unmanaged resources (such as file handles, as you mention) at the time that Dispose is called.
If the Dispose method is not called on a class instance that implements IDisposable, and it is a "proper" implementation of that interface, then when the object is finalized, the unmanaged resources will be cleaned up.
However, just because a "proper" implementation of IDisposable will guarantee that unmanaged resources will be disposed of at some point in the future, it doesn't mean that one should not call Dispose. The presence of the IDisposable interface indicates that you should call Dispose as-soon-as-possible.
If you allocate native resources yourself (not via other means of the .NET-Framework), you have to release them before you are garbage-collected. This can/should be done by using a "Finalizer", i.e. ~MyClass() which must NOT be confused with a destructor in C++.
Finalizers make GC slow, because they are executed on a separate thread and therefore require two GC cylces to get rid of an object.
Therefore you should also implement IDisposable where you free your native resources and call GC.SuppressFinalize(this).
You should not release the unmanaged resources directly unless you create you own unmanaged resource wrapper. Usually you call Dispose or Close to free the unmanaged resource. This is done to provide some control on how resources are reclaimed. If you use some class that wraps an unmanaged resource and provide some finalizer the Garbage Collector will reclaim the resource when the Finalizer Queue thread is handling the object. See Jeffrey Richter's article for more information
If you have an object that is using native resources, then you must make sure that object implements IDisposable interface. You can then call MyObject.Dispose() and in that Dispose() method, make sure you're cleaning up your native resources.
Simply setting the object to NULL is not enough since the native resource will hang around in memory.

Categories