ABB DSQC679 3HAC028357-001 關(guān)閉時(shí)從第一個(gè)視圖調(diào)用其Dispose方法
學(xué)習(xí)并應(yīng)用這些GUI控件內(nèi)存管理的一般規(guī)則。
?具有圖形表示的控件,例如ABB Numpad、TabControl、,
GroupBox、TpsLabel、ListView和Microsoft PicureBox以及
圖中的DataGrid將自動(dòng)添加到中的控件集合
初始化組件。它可能看起來(lái)是這樣的:
this.Controls.Add(this.numPad1);
?如果上圖代表應(yīng)用程序的第一個(gè)視圖,則控件
的基類處理
當(dāng)應(yīng)用程序關(guān)閉并且Dispose方法為
由TAF調(diào)用。當(dāng)Dispose中的以下語(yǔ)句
方法被執(zhí)行:base.Dispose(Dispose);
?但是,如果它代表應(yīng)用程序的輔助視圖(即
實(shí)際上是這種情況,從命令上的關(guān)閉按鈕可以看出
bar),則必須在其關(guān)閉時(shí)從第一個(gè)視圖調(diào)用其Dispose方法。
然后,它的基類將刪除作為其控件一部分的所有控件
集合,就像前面的情況一樣。
?GUI控件沒(méi)有圖形表示,但位于
表單下的組件窗格,例如GTPUSaveFileDialog,
RapidDataBindingSource、AlphaPad等未添加到
默認(rèn)情況下控制集合。這些是你需要的
刪除時(shí)要特別小心,因?yàn)槔占饔肋h(yuǎn)不會(huì)收集它們。
如果您忘記對(duì)此類控件顯式調(diào)用Dispose,您將導(dǎo)致
永久性內(nèi)存泄漏。仔細(xì)研究下一節(jié)中的代碼示例
部分

Learn and apply these the general rules for memory management of GUI controls.
? Controls with a graphical representation, e.g the ABB Numpad, TabControl,
GroupBox, TpsLabel, ListView and the Microsoft PicureBox and
DataGrid in the figure, are automatically added to the controls collection in
InitializeComponent. It may look like this:
this.Controls.Add(this.numPad1);
? If the preceding figure represents the first view of your application, controls
with graphical representation will be disposed of by the base class of your
view class when your application is shut down and the Dispose method is
called by TAF. This happens when the following statement in your Dispose
method is executed: base.Dispose(disposing);
? If, however, it represents a secondary view of your application (which is
actually the case here, as you can tell from the close button on the command
bar), you must call its Dispose method from the first view when it is closed.
Its base class will then remove all controls that are part of its controls
collection, like in the previous case.
? GUI controls that have no graphical representation, but are located in the
Components pane under the form, for example GTPUSaveFileDialog,
RapidDataBindingSource, AlphaPad and so on are NOT added to the
controls collection by default. These are the ones that you need to be
especially careful to remove, as no garbage collector will ever gather them.
If you forget to explicitly call Dispose on such controls you will have caused
a permanent memory leak. Carefully study the code example in the next
section.