[Clean up unmanaged resources] implement the dispose method

Source: Internet
Author: User

 

The release object mode (called the release mode) specifies the lifetime of the object.

Type dispose
Method should release all its resources. It should also call its parent-type dispose
Method to release all resources of the base type. Parent-type dispose
The method should release all its resources and then call its parent-type dispose.
Method to spread this mode throughout the base type hierarchy. To help ensure that resources are always correctly cleared, dispose
The method should be called multiple times without exception.

Implement dispose for types (such as arrays) that only use managed resources
The method cannot improve the performance because these types are automatically recycled by the garbage collector. It is necessary to primarily host objects that use local resources and COM that are made public to. NET Framework
Use dispose
Method. Use the managed object of local resources (such as filestream
Class) Implement idisposable
Interface.

Important

C ++
Programmers should not use this topic. See Destructors and
Finalizers in Visual C ++. In. NET Framework 2.0, C ++
The compiler supports deterministic disposal of resources and does not allow direct implementation of dispose.
Method.

Dispose
The method should call suppressfinalize for the object to be released.
Method. Suppressfinalize
Will block the call to its finalize
Method. Remember to execute
Finalize
This method reduces the performance. If
Dispose
The method has completed object cleanup, so the garbage collector does not have to call the Finalize of the object.
Method.

For GC. keepalive
The code example provided by the method demonstrates how to force garbage collection to cause the terminator to run while the recycle object member is still executing. In a longer dispose
It is best to call keepalive at the end of the Method
Method.

Example

The following code example demonstrates how to implement dispose for classes that encapsulate unmanaged resources.
Recommended design mode of the method.

The Resource class is usually from a complex local class or API
And must be customized. Use this code mode as a starting point for creating a resource class and provide necessary customization based on encapsulated resources.

 Imports system <br/> imports system. io <br/> class Program </P> <p> Public shared sub main () <br/> try <br/> 'initialize a stream resource to pass <br/> 'to the disposableresource class. <br/> console. write ("Enter filename and its path:") <br/> dim filespec as string = console. readline <br/> dim FS as filestream = file. openread (filespec) <br/> dim testobj as disposableresource = new disposableresource (FS) </P> <p> 'use the resource. <br/> testobj. dosomethingwithresource () </P> <p> 'pose theresource. <br/> testobj. dispose () </P> <p> catch e as filenotfoundexception <br/> console. writeline (E. message) <br/> end try <br/> end sub <br/> end class </P> <p> 'this class shows how to use a disposable resource. <br/> 'the resource is first initialized and passed to <br/> 'the constructor, But it coshould also be <br/>' initialized in the constructor. <br/> 'the lifetime of the resource does not <br/> 'exceed the lifetime of this instance. <br/> 'this type does not need a finalizer because it does not <br/> 'directly create a native resource like a file handle <br/> 'or memory in the unmanaged heap. <br/> public class disposableresource <br/> implements idisposable </P> <p> private _ resource as stream </P> <p> private _ disposed as Boolean </P> <p> 'the stream passed to the constructor <br/> 'must be readable and not null. <br/> Public sub new (byval stream as stream) <br/> mybase. new () <br/> If (stream is nothing) Then <br/> throw new argumentnullexception ("stream is null. ") <br/> end if <br/> if not stream. canread then <br/> throw new argumentexception ("stream must be readable. ") <br/> end if <br/> _ resource = stream <br/> dim objtypename as string = _ resource. getType. tostring <br/> _ disposed = false <br/> end sub </P> <p> 'demonstrates using the resource. <br/> 'It must not be already disposed. <br/> Public sub dosomethingwithresource () <br/> If _ disposed then <br/> throw new objectdisposedexception ("resource was disposed. ") <br/> end if </P> <p> 'Show the number of bytes. <br/> dim numbytes as integer = ctype (_ resource. length, integer) <br/> console. writeline ("number of bytes: {0}", numbytes. tostring) <br/> end sub </P> <p> Public overloads sub dispose () implements idisposable. dispose <br/> dispose (true) </P> <p> 'use supressfinalize in case a subclass <br/> 'of this type implements a finalizer. <br/> GC. suppressfinalize (me) <br/> end sub </P> <p> protected overridable overloads sub dispose (byval disposing as Boolean) <br/> if not _ disposed then </P> <p> 'if you need thread safety, use a lock around these <br/> 'operations, as well as in your methods that use the resource. <br/> If disposing then <br/> If (not (_ Resource) is nothing) Then <br/> _ resource. dispose () <br/> end if <br/> console. writeline ("object disposed. ") <br/> end if </P> <p> 'indicates that the instance has been disposed. <br/> _ resource = nothing <br/> _ disposed = true <br/> end if <br/> end sub <br/> end class </P> <p>

 

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.