Dispose模式与Finalize, Dispose方法实现准则

Dispose模式与Finalize, Dispose方法实现准则,第1张

概述最近一直纠结于一个Silverlight程序的内存泄露问题,顺便复习了一下Dispose模式 及Finalize, Dispose方法的实现准则 :   Finalize实现准则: The following rules outline the usage guidelines for the Finalize method: Only implement Finalize on objects

最近一直纠结于一个Silverlight程序的内存泄露问题,顺便复习了一下Dispose模式 及@L_502_1@ :

 

Finalize实现准则:

The following rules outline the usage guIDelines for the Finalize method:

Only implement Finalize on objects that require finalization. There are performance costs associated with Finalize methods. If you require a Finalize method,you should consIDer implementing Idisposable to allow users of your class to avoID the cost of invoking the Finalize method. Do not make the Finalize method more visible. It should be protected,not public . An object's Finalize method should free any external resources that the object owns. Moreover,a Finalize method should release only resources that are held onto by the object. The Finalize method should not reference any other objects. (因为GC回收各对象的顺序不定,"other objects"可能已经被回收了,再去引用,可能会有异常) Do not directly call a Finalize method on an object other than the object's base class. This is not a valID operation in the C# programming language. Call the base.Finalize method from an object's Finalize method.
@H_404_57@Note    The base class's Finalize method is called automatically with the C# and the Managed Extensions for C++ destructor Syntax.
dispose实现准则:

The following rules outline the usage guIDelines for the dispose method:

Implement the dispose design pattern on a type that encapsulates resources that explicitly need to be freed. Users can free external resources by calling the public dispose method. Implement the dispose design pattern on a base type that commonly has derived types that hold on to resources,even if the base type does not. If the base type has a close method,often this indicates the need to implement dispose . In such cases,do not implement a Finalize method on the base type. Finalize should be implemented in any derived types that introduce resources that require cleanup. Free any disposable resources a type owns in its dispose method. After dispose has been called on an instance,prevent the Finalize method from running by calling the GC.SuppressFinalize Method . The exception to this rule is the rare situation in which work must be done in Finalize that is not covered by dispose . Call the base class's dispose method if it implements Idisposable . Do not assume that dispose will be called. Unmanaged resources owned by a type should also be released in a Finalize method in the event that dispose is not called. Throw an ObjectdisposedException from instance methods on this type(other than dispose ) when resources are already disposed. This rule does not apply to the dispose method because it should be callable multiple times without throwing an exception. Propagate the calls to dispose through the hIErarchy of base types. The dispose method should free all resources held by this object and any object owned by this object. For example,you can create an object like a TextReader that holds onto a Stream and an EnCoding,both of which are created by the TextReader without the user's kNowledge. Furthermore,both the Stream and the EnCoding can acquire external resources. When you call the dispose method on the TextReader,it should in turn call dispose on the Stream and the EnCoding,causing them to release their external resources. You should consIDer not allowing an object to be usable after its dispose method has been called. Recreating an object that has already been disposed is a difficult pattern to implement. Allow a dispose method to be called more than once without throwing an exception. The method should do nothing after the first call. 总结

以上是内存溢出为你收集整理的Dispose模式与Finalize, Dispose方法实现准则全部内容,希望文章能够帮你解决Dispose模式与Finalize, Dispose方法实现准则所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1030439.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存