Fix: Attempted to use an object that has ceased to exist.

Last week, I faced this issue for few hours trying to fix this issue since the error message is a bit generic and does not tell you which object that has been ceased. It is difficult to drill down the root of the issue.

The issue will look like this:

attempted-to-use-an-object-that-has-ceased-to-exist

Cause:

In my scenario, I used SPContext object to initialise SPSite object in using statement.

When the code goes outside using scope statement, it will dispose SPContext which is NOT acquired back.

using (SPSite site = SPContext.Current.Site)
{
   using (SPWeb web = site.RootWeb)
   {
      // your custom code
   }
}

Resolution:

Update your code to look like below:

using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
   using (SPWeb web = site.RootWeb)
   {
      // your custom code
   }
}
Advertisements

Tagged: , ,

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: