Two versions of a function. One works. The other doesn't

I had some problems with this function causing too much garbage, and as a result, the GC ran too often. So I changed it, so I wouldn’t create a new List, but just fire the functions of the list during the search.

As you can see in the first version, I obtain a new List only holding valid objects, and then fire them one by one afterwards. In the second version, the new version, I just iterate through the original list, and fire the functions whenever the same requirements as in version one has been met. But it doesn’t work. And I simply don’t understand why, and I’m sure I’m overlooking something. Please have a look, and help me out here :slight_smile:

Version 1. Works perfectly, but generates a little too much garbage due to a new List being generated.

public void UpdateOutputs(Activator.Sender output, List<DataWrapper> data = null, PageObject specificReceiver = null)
    {       
        
        List<Activator> outputActivator = null;
        
        if (specificReceiver == null) {
            
            outputActivator = activators.FindAll(c => c.sender == output);

        } else {
            
            outputActivator = activators.FindAll(c => c.sender == output  c.receiverOwner == specificReceiver);
        }

        if (outputActivator != null)
        {
            for (int i = 0; i < outputActivator.Count; i++) {
                
                outputActivator[i].DoSend(data);
            }

        }

    }

Version 2. Doesn’t work. The system will freeze in what feels like an infinite loop, though it doesn’t seem to be this function being stuck. I tried making a bailOut procedure, to make sure the loop had a way of exiting, but that didn’t help

public void UpdateOutputs(Activator.Sender output, List<DataWrapper> data = null, PageObject specificReceiver = null)
    {
        
        for (int i = 0; i < activators.Count; i++) {

            if (specificReceiver == null) {

                if (activators[i].sender == output) {
                    
                    activators[i].DoSend(data);

                }



            } else {

                if (activators[i].sender == output  activators[i].receiverOwner == specificReceiver) {
                    
                    activators[i].DoSend(data);

                }

            }

        }        

    }

Do you see any difference that could cause this?

Hmm i cant see a problem really but why not use the first one.

In the second on you perform your specificReceiver == null or if (activators.sender == output activators*.receiverOwner == specificReceiver)* in each iteration, which seems silly as you want to use only one of the two tests as it shows in the first script.

One thing I’m noticing is that in the first version, you generate the list of which activators to call before calling any of them, and in the second it happens simultaneously. This makes me suspect your problem is that one of the activators is doing something in DoSend that changes the list of activators. Have you tried stepping through the second version and seeing if the loop ends when it should? And could any of your activators result in adding another activator to the list?

Without all the information its a bit hard for us to answer that. Why not step through the code in the debugger?