简体   繁体   English

从列表中删除C#对象会丢失引用

[英]c# object loses reference when removed from list

I have a very strange issue. 我有一个很奇怪的问题。 Basically I have created a class called TreeNode that represents a node in a tree. 基本上,我创建了一个名为TreeNode的类,该类表示树中的节点。 I then create the tree by adding all the nodes to a List. 然后,通过将所有节点添加到列表中来创建树。

class TreeNode
{

    private TreeNode parent, lChild, rChild;
    private int key, val;

    public int Key
    {
        get { return key; }
        set { key = value; }
    }
    public int Val
    {
        get { return val; }
        set { val = value; }
    }

    public TreeNode Parent
    {
        get { return parent; }
        set { parent = value; }
    }
    public TreeNode LChild
    {
        get { return lChild; }
    }
    public TreeNode RChild
    {
        get { return rChild; }
    }

    public TreeNode(int k, int v)
    {
        key = k;
        val = v;
    }

    public void SetChild(TreeNode leftChild, TreeNode rightChild)
    {
        this.lChild = leftChild;
        this.rChild = rightChild;

    }

    public bool isLeaf()
    {
        if (this.lChild == null && this.rChild == null) 
        {
            return true;
        } else 
        {
            return false;
        }
    }

    public bool isParent()
    {
        if (this.parent == null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public void SetParent(TreeNode Parent)
    {
        this.parent = Parent;
    }
}

So if i put a breakpoint just after the creation of the tree and hover over the list in Visual Studio I can see the structure of the tree - with all the references working perfectly from the root down to the leaves. 因此,如果我在创建树之后立即放置一个断点,并将鼠标悬停在Visual Studio中的列表上方,则可以看到树的结构-所有引用从根到叶都可以正常工作。

If however I do the following: 但是,如果执行以下操作:

TreeNode test = newTree[newTree.Count - 1];

please note: 请注意:

private List<TreeNode> newTree = new List<TreeNode>();

which returns the root node - and hover over again I can do down one level (ie left child or right child) but these children do not have any references for their children after that. 它返回根节点-并再次悬停在上面我可以向下一级(即,左子级或右子级)进行操作,但是这些子级之后对它们的子级没有任何引用。

I'm wondering if I'm losing the reference in memory to the other nodes in the list as the test node is not part of the list? 我想知道是否由于测试节点不在列表中而丢失内存中对列表中其他节点的引用?

Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks Tom 谢谢汤姆

you sure you don't have (note no space between new and tree in your code) 您确定没有(请注意代码中new和tree之间没有空格)

TreeNode test = new Tree[newTree.Count - 1]; 

Which would create a new empty array of tree (probably not what you intended), and leave your original tree un-rooted and inaccessible. 这将创建一个新的空树阵列(可能不是您想要的),并使原始树无根且不可访问。

Can you make sure your code is correct, please? 您能确定您的代码正确吗?

Seems as though I found the issue - I wasn't correctly updating some of the parent nodes with their relevant child nodes - problem solved. 似乎我发现了问题-我没有正确地用父节点的相关子节点更新某些父节点-问题已解决。

Thanks for your help Tom 谢谢汤姆的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM