C#. NET for unmanaged code cleanup

Source: Internet
Author: User
Tags implement resource

These two days help other project team review code, found some places to implement the IDispose interface, but also found some questions about IDispose:

1.A type implements the IDispose interface, B type contains a type of field, B type does not implement IDispose interface

2. The finalize finalizer is implemented in a class, and the IDispose interface is implemented, but the gc.suppressfinalize (this) method is not invoked inside the Dispose method.

Below I have the above two questions analysis separately, and proposes the solution.

Question 1

If the type A has unmanaged resources that need to be freed in the implemented IDispose interface, it is not convenient for type B users to release a type of unmanaged resource because the type B does not implement the IDispose interface. In this case, it is possible to forget to release type a unmanaged resources.

Solution:

Implements the Type B IDispose interface, which invokes a type a Dispose method inside the Dispose method. This way, the type B user calls the type a Dispose, while calling Type B Dispose.

Question 2

What is wrong with not calling the Gc.suppressfinalize (this) method in the Dispose method, which causes the garbage collector to not recycle the object of this type in a timely manner. When the GC begins to work, it first removes the garbage object without finalizers from memory, and all objects with finalizers are added to a garbage queue. The GC invokes a new thread to execute the finalizer of these objects. When the finalizer is finished, the object is removed from the queue. Once this object is removed from the queue, the object can be reclaimed when the GC starts working again, so objects with finalizers are kept longer in memory than they do. I'll give you a detailed description of this in the back.

Solution:

Call the Gc.suppressfinalize (this) method in the Dispose method. In this way, objects with finalizers are not added to the garbage queue.

Cut to the chase.

In. NET, there are two ways in which unmanaged code can be cleaned: Finalize method and Dispose method.

Finalize method: Frees a non-pass resource by implementing a finalize approach to the custom type.

Starting with. net2.0, the C # compiler cannot invoke and override the display of finalize, and it must be implemented using a destructor.

class A
{
~A()
{
释放资源;
}
}

The code above is to release resources in a finalize way like C + +, which frees up resources with destructors.

However, it is implemented in a different way than C + + because it is managed by the garbage collector.

As you can see, it is easy to release unmanaged resources in a finalize manner, but if you understand how he implements it, you may not choose to use it to release unmanaged resources.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.