2015-05-18 20 views
0

我想知道下面的代碼是否有可能的循環引用。我不確定垃圾收集在C#中的工作原理。爲了清晰起見,我爲響應者添加了行號。 Line 16 in mainForm.cs讓我很擔心,因爲您可以看到,VerticalText是容器中的一個項目,並且對它所在的容器具有('參考')。它變得棘手,因爲我的項目/ object由UI元素Panel和Data Persistce元素擁有列表<>其他可能的循環引用位於行14mainForm.cs其中擁有的成員變量獲取指向其所有者的指針。C#循環引用的所有者和擁有對象間接嗎?

我做了我的研究,發佈這個問題之前,只是想出了含糊不清的答案。這段代碼是一個大型程序的一部分,它的工作原理。即使通過加強與調試,我可以看到,這VerticalText對象刪除自身在線24的VerticalText.cs因爲當上線25調試步驟,列表<的計數>由1

遞減

我還包括其他的問題,可以提高微妙的問題,在註釋的形式線5和在VerticalText.cs這都不是很重要,但將是有益的。 這是一個文字板式應用程序,它具有可旋轉的文本,表示爲VerticalText類的實例,每個類都存在於TransparentPanel類中。每個實例都有幾個按鈕,其中一個允許它自己刪除。重點是與該實例的面板和存儲容器(列表<>)進行通信以將其移除。請讓我知道我的代碼是否會產生內存泄漏或循環引用。我希望沒有人將此標記爲重複,而是花時間給我一個答案。謝謝。

1 // mainForm.cs 
2 // Member vars. are 'public' just for this example. In program, they are private with public properties 
3 namespace My_Note 
4 { 
5  public partial class MainForm : Form 
6  { 
7  private List<VerticalText>m_verticalTextList = new List<VerticalText>(); // Container of VerticalText 
8   
9  // transparentPanel is a 'Panel' in the 'MainForm', it can hold several instances of VerticalText 
10  transparentPanel_MouseUp(object sender, MouseEventArgs e) 
11  { 
12   VerticalText nextText = new VerticalText(e); 
13   nextText.OwnerTranspPanel = transparentPanel; // trying to pass a 'pointer/reference' 
14   nextText.OwnerRichTextBox = richTextBox; // trying to pass a 'pointer/reference' 
15   nextText.OwnerBackPanel = backPanel; // trying to pass a 'pointer/reference' 
16   nextText.OwnerVerticalTextList = m_verticalTextList; /* pointer/reference to the object 
17                 that will own me (Good or bad?) */ 
18 
19   m_verticalTextList.Add(nextText); 
20   transparentPanel.Controls.Add(nextText.MoveButton); 
21   transparentPanel.Controls.Add(nextText.OptionsButton); 
22   transparentPanel.Controls.Add(nextText.DeleteButton); 
23   transparentPanel.Controls.Add(nextText.RotateButton); 
24  } 
25  // Some other code... 
26 } 
26 } 

1 // VerticalText.cs 
2 // Member vars. are 'public' just for this example. In program, they are private with public properties 
3 namespace My_Note 
4 { 
5  class VerticalText // should this be public, private, internal, or can it just stay the way it is? 
6  { 
7  public TransparentPanel OwnerTransparentPanel // 'pointer/reference' to my owner's other object 
8  public RichTextBox OwnerRichTextBox; // 'pointer/reference' to my owner's other object 
9  public Panel OwnerBackPanel; // 'pointer/reference' to my owner's other object 
10  public List<VerticalText>OwnerVerticalTextList; /* 'pointer/reference' to my owner's other object 
11               which also contains me (this). */ 
12  private Button m_deleteButton = new Button(); // One of several buttons 
13 
14  m_deleteButton_MouseUp(object sender, MouseEventArgs e) 
15  { 
16   foreach (VerticalText v in OwnerVerticalTextList) 
17   { 
18    if (v == this) 
19    { 
20    OwnerTranspPanel.Controls.Remove(m_moveButton); 
21    OwnerTranspPanel.Controls.Remove(m_optionsButton); 
22    OwnerTranspPanel.Controls.Remove(m_deleteButton); 
23    OwnerTranspPanel.Controls.Remove(m_rotateButton); 
24    OwnerVerticalTextList.Remove(v); // Am I allowed to delete myself here? 
25    return; // If I deleted myself, then why does this still work? 
26    } 
27   } 
28  } 
29  // Some other code... 
30 } 
31 } 
+0

我用來避免循環引用的方式,並且在子類中有一個指向父類的指針使用'WeakReference'。你可以有一個變量,它是'WeakReference'和一個返回真實對象的屬性。在https://msdn.microsoft.com/en-us/library/system.weakreference%28v=vs.110%29.aspx – CrApHeR

+0

中有更詳細的信息非常好。我想它被稱爲顯式聲明。我想這適用於父子關係以及彼此沒有直接關係的對象。 –

+0

1.使用內存分析器檢查您的應用程序是否有內存泄漏或不存在。這是唯一的方法。 2.爲什麼你擔心循環引用? –

回答

相關問題