RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Preferred method for IPooledEnumerable

kevin10

Sorceror
What is preferred?

Code:
IPooledEnumerable eable  = this.GetMobilesInRange(5);
foreach (Mobile m in eable)
{
    Do stuff;
}
eable.Free();

Or

Code:
foreach (Mobile m in this.GetMobilesInRange(5))
{
    DoStuff;
}
 

_Epila_

Sorceror
They will both have the same result. On the second method, during the code execution, the pre-processor (or whatever C# does internally) will have to store the list somewhere in order to loop it, you may get about 3 or 4 less instructions on your IL code (or whatever, I'm not .NET expert :p) nothing to worry about

But I would suggest you to use the first example, since RunUO's default codes uses it, you may also notice after the execution is done, they call eable.Free(). I don't really know why and what it does (I've not peeked this part of the code yet) but if it is there, there is a reason. I'd guess it will free the memory used by the eable

(Scripts/Spells/Seventh/ChainLightning.cs)
C#:
                if ( map != null )
                {
                    IPooledEnumerable eable = map.GetMobilesInRange( new Point3D( p ), 2 );
 
                    foreach ( Mobile m in eable )
                    {
                        if ( Core.AOS && m == Caster )
                            continue;
 
                        if ( SpellHelper.ValidIndirectTarget( Caster, m ) && Caster.CanBeHarmful( m, false ) )
                        {
                            if ( Core.AOS && !Caster.InLOS( m ) )
                                continue;
 
                            targets.Add( m );
 
                            if ( m.Player )
                                playerVsPlayer = true;
                        }
                    }
 
                    eable.Free();
                }
 

kevin10

Sorceror
Right, that's what I thought. It's safe to say that most code uses example 2, however. I suspect that will be changed on future commits to the RunUO Github.
 

frostshoxx

Sorceror
Hi there,

Looks like the method map.GetMobilesInRange() returns IPooledEnumerable type of object, which requires explicit calls to .Free() method after it's done to clean up the memory.

I don't think foreach() loop will implicitly call .Free() method, though (it's not the using() statement).

Would someone be able to confirm this?
 
Top