我實際上正在開發(fā)由 GTK# 2.12 提供支持的軟件,我們有一些內(nèi)存泄漏,我正在尋找如何改進(jìn)它。我制作了一個小應(yīng)用程序來測試擺脫小部件的正確方法。我正在創(chuàng)建 10 000 個標(biāo)簽,然后將它們刪除。這是我的標(biāo)簽類:namespace test_gtk_objects{ public class TimeLabel : Label { private bool disposed = false; protected DateTime _time; public TimeLabel(DateTime time) { Time = time; } ~TimeLabel() { Dispose(false); Console.WriteLine("Called dispose(false) of class" + this.ToString()); GC.SuppressFinalize(this); } //dispose public override void Dispose() { // Console.WriteLine("Called dispose(true) of class" + this.ToString()); Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposed) return; if (disposing) { Hide(); Unrealize(); Unparent(); Unmap(); Destroy(); Unref(); Console.WriteLine("ref: " + RefCount.ToString()); // Free any other managed objects here. // } disposed = true; base.Dispose(); } protected void display(Label input_label) { input_label.Text = _time.ToString("HH:mm:ss"); } internal DateTime Time { get { return _time; } set { if (_time != value) { _time = value; display(this); } } } }}如您所見,我已按照建議覆蓋了 dispose 方法。我還嘗試了一些方法來釋放大部分小部件(尤其是使用 unref() )以確保小部件沒有鏈接到其他東西。在我的主窗口中,我只是在 à 列表中創(chuàng)建這些標(biāo)簽,然后通過對每個標(biāo)簽調(diào)用 Dispose 方法來刪除它們。我所有的小部件似乎都被刪除了,但我總是有一些Glib。切換引用,Glib。您可以在我的內(nèi)存快照中看到的信號:如果你能幫我解決這個問題,我不知道如何擺脫它,我將不勝感激
1 回答

Qyouu
TA貢獻(xiàn)1786條經(jīng)驗 獲得超11個贊
當(dāng)我通過在 destroy 方法中調(diào)用 Glib Dispose 來銷毀小部件時,我最終編輯了 GTK 銳利庫以刪除切換引用。我的對象數(shù)量不會快速增長,并且在需要時我的所有對象都會被刪除!
- 1 回答
- 0 關(guān)注
- 258 瀏覽
添加回答
舉報
0/150
提交
取消