0

OK, so I have created a dll project. This dll project uses a tree class structure to hold data, and has functions that are used against that data.

so in the dll project we have:

namespace Engine
{
   public class Engineclass
   {
       public Component SolutionTree = new Component();

       public Component ReturnComponent(string ComponentName)
       {
           Component CurrentComp = SolutionTree;
           if(CurrentComp.Name != ComponentName)
           {
               CurrentComp = RecurseForName(CurrentComp, ComponentName);
           }

           return CurrentComp;
        }

        public Component RecurseForName(Component CompORG, ComponentName)
        {
            Component FoundComp = null;

            if (!string.IsNullOrEmpty(ComponentName) && (CompORG != null))
            {
                if (CompORG.ComponentName.ToUpper().Trim() ==
                    ComponentName.ToUpper().Trim())
                {
                    return CompORG;
                }

            foreach (var CompNew in CompORG.ActiveComponents)
            {
                var FoundComp2 = RecurseForName(CompNew, ComponentName);
                if (FoundComp2 != null)
                {
                    return FoundComp2;
                }
            }
        }

        return FoundComp;
        }
    }
}

The Component class is:

public class Component
{
    public string Name {get; set;}
    public List<Component> ActiveComponents { get; set; }

    public Component()
    {
        this.ActiveComponents = new List<ActiveComponent>();
    }
}

Now in my Console app I am testing it on, I load the dll and populate the solution tree. i.e.

EngineClass EC = new EngineClass();

EC.Component NewComp = new EC.Component();

EC.Name = "bob";

EC.Component NewSubComp = new EC.Component();

EC.Name = "Son of Bob";

NewComp.ActiveComponents.add(NewSubComp);

EC.SolutionTree = NewComp;

All works fine.

BUT, when I try to use the recursive function to return a specific component I get a System.NullReferenceException: 'Object reference not set to an instance of an object.'

var FoundASon = EC.FindComponent("Son of Bob");

and it occurs on the return CompORG; line in the RecurseForName function.

So, I know CompOrg is not null, and the name in CompOrg has matched, but trying to return CompOrg causes the nullreferenceexception!

I really need to help on this issue, as it is so hard to debug a compiled dll.

p.s. the dll project has unit tests and they pass the functions as working just fine.

Nik P
  • 2,693
  • 9
  • 21
  • 3
    The local variables of the dll are on the execution stack and once you return from the method the stack is no longer valid. So you have two choices 1) The memory for the tree has to be declared in the calling method so when you return from the method the tree memory is not disposed. 2) Use the Window Allocate method to allocate the tree memory which will stay valid until the memory is disposed. Be careful this method can cause memory leaks if the tree is not disposed. – jdweng Jun 03 '20 at 13:41
  • Sometimes exceptions are not reported for the correct line. A guess is that some node has a Name that is null. I would suggest initializing Name to an empty string in the constructor and see if that helps. – JonasH Jun 03 '20 at 14:24
  • The nullreference link is nice, doesn't solve the issue as the issue seems to be more linked to jdwengs explanation. Trouble is, it still doesn't help me fix the issue, I am struggling to find examples on passing data around in functions inside a dll (specifically recursively). Are there any links or code to the issue of passing objects around inside a dll? I'm digging on the web, but not finding answers that help me to solve the problem. Would be useful for others stubbling across this question as well. Many thanks for the answers so far, but I'm still not close to fixing the issue. – DarftNader Jun 04 '20 at 08:29

0 Answers0