SPSite and SPWeb is not closed properly in IPAccessHelper.cs

Jul 20, 2010 at 3:06 AM

The following code block in IpAccessHelper.cs does not close SPSite and SPWeb properly:
public SPFile FormFile
        {
            get
            {
                if (_formFile == null)
                {
                    SPSite site = new SPSite(WorkflowProperties.SiteId);

                    SPWeb web = site.AllWebs[WorkflowProperties.WebId];

                    _formFile = web.GetFile(WorkflowProperties.ItemUrl);
                }

                return _formFile;
            }
        }

Should be:

public SPFile FormFile
        {
            get
            {
                if (_formFile == null)
                {
                    using(SPSite site = new SPSite(WorkflowProperties.SiteId))
      {
                     using(SPWeb web = site.AllWebs[WorkflowProperties.WebId])
   {
                     _formFile = web.GetFile(WorkflowProperties.ItemUrl);
   }
      }
                }

                return _formFile;
            }
        }

Sep 7, 2010 at 1:52 PM

why not just use the instance of SPWeb or SPSite from the workflow context rather than create a new one?