Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 105467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:26:38+00:00 2026-05-11T01:26:38+00:00

Say I’m working with Sharepoint (this applies to other object models as well) and

  • 0

Say I’m working with Sharepoint (this applies to other object models as well) and in the middle of my statement, I call a method, in this case ‘OpenWeb()’, which creates an IDisposable SPWeb object. Now, I cannot call Dispose() on the SPWeb object because I don’t have the reference to it. So do I need to be concerned about this leaking memory?

SPUser spUser = SPControl.GetContextSite(HttpContext.Current).OpenWeb().SiteUsers[@'foo\bar']; 

I know that I could have broken up the statement into multiple lines and get the SPWeb reference to call Dispose:

SPWeb spWeb = SPControl.GetContextSite(HttpContext.Current).OpenWeb(); SPUser spUser = spWeb.SiteUsers[@'foo\bar']; spWeb.Dispose(); 

Please keep in mind that my question is not about aesthetics, but more about what happens to the IDisposable object that I cannot explicitly call Dispose() on, since I don’t have the reference.

Sorry about not being clear enough when I first asked the question. I’ve since rephrased it. Thanks for all the responses so far.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:26:38+00:00Added an answer on May 11, 2026 at 1:26 am

    ‘what happens to the IDisposable object that I cannot explicitly call Dispose() on?’

    In general you can call Dispose (either implicitly with a using statement or explicitly) on all disposable objects, however in the hypothetical scenario where you can not, it depends on the way the object was implemented.

    In general, the .Net objects will follow pattern along these lines. The pattern is to define a finalizer that cleans stuff up in case dispose is not called and then have dispose suppress the finalizer. Which reduces memory load and gives the GC less work to do.

    Amongst the many problems with calling Dispose from the finalizer, is that you are turning a single threaded problem into a multithreaded problem, the finalizer will run on a different thread which can expose some very subtle and hard to catch bugs. Also, it means you will be holding on to unmanaged resources for longer than expected (Eg. you open a file, forget to call close or dispose and next time you go to open it its locked)

    Bottom line is, it is a best practice to dispose all disposable objects, otherwise you can introduce weird and complex bugs. With the caveat that some frameworks, like sharepoint, return shared instances of objects that should not be disposed according to the documentation.

    I usually find the code much more readable when I dispose my objects with the ‘using’ pattern. The problem with calling Dispose explicitly (object.Dispose()) is that it can be hard to trace where the object was allocated and it is very easy to forget. You can not forget to close the curly bracket of the using statement, the compiler will complain 🙂

    EDIT / GOTCHA

    According to MS documentation You should not call dispose on references to share point shared objects which are returned by GetContextSite. So, you should be extra careful here.

    See this answer for a safe sharepoint pattern you should use.

    However, if you have a reference to a shared resource, such as when the object is provided by the GetContextSite method in a Web Part, do not use either method to close the object. Using either method on a shared resource causes an Access Violation error to occur. In scenarios where you have a reference to a shared resource, instead let Windows SharePoint Services or your portal application manage the object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer There was a question on Stackoverflow that asked something similar… May 11, 2026 at 10:06 am
  • added an answer Should be calloc(4, sizeof(*port_data)): Note * before var name. May 11, 2026 at 10:06 am
  • added an answer Drupal would be a very good choice for this. It… May 11, 2026 at 10:06 am

Related Questions

Say I have the following file structure: app/ app.py controllers/ __init__.py project.py plugin.py If
Say I've got this array: MyArray(0)=aaa MyArray(1)=bbb MyArray(2)=aaa Is there a .net function which
Say I have an ASMX web service, MyService. The service has a method, MyMethod.
Say I have a class named Frog, it looks like: public class Frog {
Say I have three files (template_*.txt): template_x.txt template_y.txt template_z.txt I want to copy them
Say I have two strings, String s1 = AbBaCca; String s2 = bac; I
Say I have a Student table, it's got an int ID. I have a
Say I'm building an ASP.Net class that inherits from IHttpHandler , should I wire
Say I have several JavaScript includes in a page: <script type=text/javascript src=/js/script0.js></script> <script type=text/javascript
Say I have a Textbox nested within a TabControl . When the form loads,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.