[mapguide-users] RE: featureService.SelectFeatures and memory consumption

Pietro Ianniello pietro.ianniello at gmail.com
Tue Aug 17 05:46:35 EDT 2010


You should try to dispose every mapguide object.
I have written the following simple class not to forget any object:

---------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MapGuideHelpers
{
    //Class T[o]B[e]Disposed
    public class MGDisposable<U> : IDisposable where U :
OSGeo.MapGuide.MgDisposable
    {
        U _val;
        public U Get { get { return _val; } }

        MGDisposable(U val)
        {
            _val = val;
        }

        public static implicit operator MGDisposable<U>(U uvar)
        {
            return new MGDisposable<U>(uvar);
        }
        public static explicit operator U(MGDisposable<U> mgtbdvar)
        {
            return mgtbdvar._val;
        }

        #region disposing interface

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~MGDisposable()
        {
            this.Dispose(false);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (null != _val)
            {
                _val.Dispose();
            }
        }

        #endregion
    }
}
---------------------------------

Now you can:

            using (MGDisposable<MgMAPGUIDECLASS> mgObj = <GET THE OBJECT
UING MAPGUIDE>)
            {

            }

and the use mgObj:
          mgObj.Get.<METHOD>()
or
          ((MgMAPGUIDECLASS)mgObj).<METHOD>()

Example:

            using (MGDisposable<MgLayerBase> layer =
layerCollection.GetItem(i))
            {
                using (MGDisposable<MgResourceIdentifier> layerDefinition =
*layer.Get*.GetLayerDefinition())
                {
                     //...
                }
            }
Or:
            using (MGDisposable<MgLayerBase> layer =
layerCollection.GetItem(i))
             {
                using (MGDisposable<MgResourceIdentifier> layerDefinition =
                                           *((MgLayerBase)layer)*
.GetLayerDefinition())
                 {
                     //...
                }
            }


Pietro Ianniello
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/mapguide-users/attachments/20100817/1e1f7928/attachment.html


More information about the mapguide-users mailing list