Effective C # – Item 18: Dispose end specification form.
July 20, 2011
Netease blog security alert: The system detected that your current password is less secure, for your account security, we recommend that you change your password immediately amend the timely closure
< br /> We have talked about, dealing with a cross-borrowing of non-managed resource object is very important. Now is the time to talk about how to write code to teach these classes cross-borrowing of non-memory resources. A standardized form that is convenient to use. Net framework measures to deal with the supply of non-memory resources. Your users want you to obey the canonical form. Also through the end even if the IDisposable interface to release unmanaged resources, of course, remember to call it in the user time, but if users forget, the destructor will rigorously enforce passive. It is the same and the trash collector to work to ensure that some of the necessary, you will only be because the object destructor function consisting of wear and tear. This is a good discipline measures unmanaged resources, which are necessary to get thoroughly inquire about it.
in the class inheritance links in the top end of the base class deserve the IDisposable interface to release resources. This species also deserve to add a destructor, as the last of the passive mechanism. These two measures are measures deserve is to release the virtual resources, so it derived classes can override this function to release their own resources. Only in its own derived class must override this function to release the resources only, and will certainly have to remember to call the base class of measures.
beginning of the class if you use a non-memory resources, you will certainly have to have a destructor. You can not look forward to your users always remember to call Dispose measures, or when they forget, you will lost some of the resources. This may be because they never call Dispose of false absurd, but you also have an obligation. Single non-memory resources to ensure safe release of the measures can even create a destructor. Thus, adding a destructor it!
When the trash collector runs, it will be removed from memory without destruction of worthless objects. The other objects have destructors also retained in memory. These objects are added to a queue in the destructor, trash collector will start a thread came to destruction of these objects. When the destructor thread ended its work, these worthless objects can be removed from the non-memory. Even if that required destruction of the object than the destruction of the object does not have in memory the work to be longer. But you do not have a choice. In case you are approved by the passive form, the type of cross-borrowing when your unmanaged resources, you will certainly write a destructor. But now you need not worry about functional problems, the next step to ensure that your users to more easily, and can avoid the destructor function consisting of wear and tear.
end IDisposable interface is a standardized form to tell the user and the system was held: the object you will certainly have the resources and timely release. IDisposable interface is only one measure:
public inte *** ce IDisposable
{
void Dispose ();
}
end IDisposable.Dispose () measures an obligation to end the following tasks:
1, perception of all non-managed resources.
2, perception of all managed resources (including offload some of the events).
3, set up a peace sign to identify the object has been processed. Case has been treated in a call to any action on the object, you can verify this and throws a ObjectDisposed symbol of disorder.
4, curb destruction. You want to call GC.SuppressFinalize (this) to end the final work.
end through the IDisposable interface, you write two things: first, even if the supply of a mechanism to resort to borrowing and timely release all managed resources, and the other even if you supply a standard form so that users to release unmanaged resources. This is extremely important, when you type in your end of the IDisposable interface and later on, the user will be able to avoid destruction, the wear and tear. Your class has become. Net community members appear quite good.
However, the mechanism created in you or there are some flaws. How to make a derived class clean up their own resources, while also being able to do a good resource base class clean it? (Translation: because the call to Dispose of measures, will certainly call the base class Dispose, of course, is the base class has the measure, but as I said before, we have only a symbol to identify whether the object is processed, regardless of the first call that, must have a measures can not deal with this symbol, and this risk exists) in case of overload the base class destructor, may increase their own end of the IDisposable interface, and these measures are all measures that will certainly call the base class; otherwise, the base class can not be safe the release of resources. Similarly, destruction and processing share some similar duties: almost sure you are copying a destructor measures and treatment measures between the code. As you learned in the 26 measured, and measures about not overloading the interface as you work as desired. Dispose canonical form in the third measure, by the defense through the help of a virtual function, to create a task and they are attached to the practice to release resources derived class. The center of the base class includes the interface code, the derived class supply the Dispose () virtual function may destructor to clean up resources:
protected virtual void Dispose (bool isDisposing);
measures the same time heavy destruction and end the task of handling will certainly supply, but also because it is the virtual function, it supplies all of the derived class function entry points. Derived class can override this function, the supply end to secure the release of its own resources, and call the base class function. When
isDisposing is true you may also clean up managed resources and unmanaged resources when isDisposing is false, you can only clean up unmanaged resources. Both cases, are able to call the base class Dispose (bool) measures to make it to clean up its own resources.
so you end up in the form of time, there is an easy example. MyResourceHog IDisposable class reveals the end, a destructor, and created a virtual Dispose measures:
public class MyResourceHog: IDisposable
{
/ / Flag for already disposed
private bool _alreadyDisposed = false;
/ / finalizer:
/ / Call the virtual Dispose method.
~ MyResourceHog ()
{
Dispose (false);
}
/ / Implementation of IDisposable.
/ / Call the virtual Dispose method.
/ / Suppress Finalization.
public void Dispose () < br />
{
Dispose (true);
GC.SuppressFinalize (true);
}
< br /> / / Virtual Dispose method
protected virtual void Dispose (bool isDisposing)
{
/ / Don dispose more than once .
if (_alreadyDisposed)
return;
if (isDisposing)
{
/ / TODO: free managed resources here.
}
/ / TODO: free unmanaged resources here.
/ / Set disposed flag :
_alreadyDisposed = true;
}
}
if the derived class has an additional clean-up task, let It ended Dispose measures:
public class DerivedResourceHog: MyResourceHog
{
/ / Have its own disposed flag.
private bool _disposed = false;
protected override void Dispose (bool isDisposing)
{
/ / Don dispose more than once.
if (_disposed)
return;
if (isDisposing)
{
/ / TODO: free managed resources here.
}
/ / TODO: free unmanaged resources here.
/ / Let the base class free its resources.
/ / Base class is responsible for calling
/ / GC.SuppressFinalize ()
base.Dispose (isDisposing);
/ / Set derived class disposed flag:
_disposed = true;
}
}
Notes and Italy, the derived class and base class has a deal with the situation of the symbol, which is fully passive. Reproduce symbols concealed in processing any possible false absurd, and a single type of treatment, rather than dealing with all types constitute the object. You deserve a passive treatment measures and to write destructors, objects may occur in any order, you may encounter this situation: a member of your class you call Dispose measures in the past had been processed. You never see this situation is because Dispose () is the ability to measure multiple calls. In a case had been treated on an object called the measure, nothing happens. Destructor has the same legal. Any reference to an object exists in memory, you do not detect a null reference. However, you may have referenced objects disposed of, may it have a destructor.
This is an extremely important with the introduction of the enlighten: For any coherent with the handling and resource cleanup measures, you will certainly free up resources only! Do not add any other processing tasks. You add in the handling and clean-up other tasks may have been in the lifetime of the object and mixed some serious problems. When you create an object it was born, in the trash collector to claim it when they died. You can feel when you are in the process will not Zaibai them, they sleep. You can not call on the object, the object can not call the measure. Clarify the various acting, they are like dead. Before the announcement of the death, but the object, the destructor have one last stretch. Destructor should do nothing, even clean up unmanaged resources. In case some of the measures across the destructor to be called on an object becomes, then it is revived. (Translation: the destructor is not invoked by the user, not by the. Net system call, but in place by the GC to run on additional threads) it live, but this is not good. Even if it is to wake sleepy eyes. There is a manifest example:
public class BadClass
{
/ / Store a reference to a global object:
< br /> private readonly ArrayList _finalizedList;
private string _msg;
public BadClass (ArrayList badList, one hundred good string msg)
{< br />
/ / cache the reference:
_finalizedList = badList;
_msg = (string) msg.Clone ();
}
~ BadClass ()
{
/ / Add this object to the list.
/ / This object is reachable, no
/ / longer garbage. It Back!
_finalizedList.Add (this);
}
}
When an object destructor BadClass rigorously enforce, its own reference to the overall situation of a linked list. This makes its own is up, and it has lived. To recommend to you in front of this measure would have been a number of daunting challenges. Object has been destructed, and therefore worthless collector from calling its credibility no longer need a destructor. Destructor if you truly want to reach a target, it will not win. Second, some of your resources may no longer be useful. GC is no longer removed from the memory queue that only by reference to the object destructor, but they may have a destructor. Case is so, they are likely to have not made use of. (Translation: have even said that measures to allow the use of the above objects, after the resurrection, is likely to object is not available.) Notwithstanding BadClass member still has the memory, they can be like a destructor may deal with, but the C # language not a measure that allows you to curb the destruction of order, so you can not let the construction of reliable operation. Do not test.
I have not seen such a code: with such obvious measures to revive an object, unless it is an academic exercise. I have read such a code, the destructor try to end some of the nature of the work, and finally through the destructor calls the object reference into, and thus his own resurrection. Destructor code which appears to concentrate on design and another handler in the. Then look again, the code is doing other things instead of releasing resources! These actions will be for your use of the process occurred in the late run many BUG. Excluding these measures to ensure that the destructor and Dispose () clean up resources unless measures do nothing.
In a managed environment, we need to be established for each type of a terminator. Only when the species, including non-managed species, including members of the class may end the IDisposable interface, we must create for them an end device. Even if we only need the IDisposable interface (no need to terminator), and we ended deserve full form. Otherwise, the derived class specification in the end will become extremely Dispose mixed form. We deserve to obey the form of the Dispose, which would make, including our own, we kind of users and derived from the creation of our species to survive the class developers become more rapid. The following is recommended for java often reveal some of the false fallacy of induction.
About REG, DAT, BIB, and DB files.
new “car loan discipline measures” announced six new statutory changes (reference).
