Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm working on a Custom Workflow activity. In the activity I use the WorkflowContext-object. From this object I get the current SPSite or SPWeb via the Site and Web properties of the WorkflowContext. Which I use in combination with the ListId and ItemId.

Do I need to dispose these SPSite and SPWeb objects?

My gut feeling says no, since they come from a 'context'. But I can't find a authoritative answer.

share|improve this question
    
I tried SPDisposeCheck with and without disposing. Neither resulted in a warning/error. –  Dribbel Sep 24 '13 at 7:11
    
It seems logical to not dispose context objects, since they are used by the workflow per se –  Robert Lindgren Sep 24 '13 at 7:12
    
But how can I be sure? –  Dribbel Sep 24 '13 at 7:15
    
If SPDisposeCheck is on your side, and you see no ULS logs about undisposed objects, you should be fine –  Robert Lindgren Sep 24 '13 at 7:19

1 Answer 1

yes you need to sometimes ;)! context is NOT using the current context if that makes sense! I belive the workflow runs on a seperate thread to sharepoint context! The context is the workflow context to get the values where the workflow is running under.

getting the context to get the spweb and/or spsite means sharepoint will NOT handle the disposing, depending on what method you use!

You only need to dispose of it, if you create the spsite or spweb (new thread) or if its stated in msdn as somthing you have to do manually! like creating a new workflow context.

it is stated in msdn to using the disposing! , you can use the using method or call the disposable method:

protected override void Dispose(bool disposing)
{
    base.Dispose(disposing);
    if (disposing)
    {
        wfContext.Dispose(); 
        workflowProperties.Dispose();
    }
}

// Dispose the workflow properties and workflow context when disposing the workflow

if you dont it would lead to memory leaks!

http://msdn.microsoft.com/en-us/library/ee767701(v=office.14).aspx

if it implements the IDisposable interface. Microsoft .NET requires objects that implement the IDisposable interface to properly cleanup the unmanaged resources by explicitly calling the Dispose() method when you are finished using them. That is if your creating like i said new ;)

having said that just to confuse you! ;) you do not need to dispose of the following:

workflowProperties.Web, workflowProperties.Site and workflowProperties.Item.

so somthing like:

SPContext context = SPContext.GetContext(workflowProperties.Web);

SPSite currentSite = context.Site;

as you can see im not using new, as im using the workflow properties context, so no need to dispose!

doing this tho:

WorkflowContext wfContext = new WorkflowContext();

you do need to dispose as your creating a new context workflow ;)

or somthing on the lines of:

SPSite site = new SPSite(workflowProperties.SiteId)

the above will need to implement the IDisposable method or use the using! Reason being, your creating a new thread (spsite) but only using the current workflow context for the site.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.