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

  • SEARCH
  • Home
  • 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 645651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:31:36+00:00 2026-05-13T21:31:36+00:00

I’m using a huge binary tree like structure whose nodes may or may not

  • 0

I’m using a huge binary tree like structure whose nodes may or may not make use of unmanaged resources. Some of these resources may take up a lot of memory, but only a few of them will be in use at a time. The initial state of the tree can be seen as ‘dormant’.

Whenever a node is accessed, that specific node and its children will ‘wake up’ and lazily acquire their assigned resources. Likewise, accesing a different branch in the tree will put the currently active branch to sleep causing its resources to be released. This means that any given node can be woken up and put to sleep once and again at any given time.

I’m currently taking advantage of the IDisposable interface to achieve this. It’s being quite useful because there are a lot of cases where I need to create small branches that will be used locally, and the ‘using’ keyword comes really handy ensuring that no resource will be left open accidentally.

Am I ok implementing IDisposable on objects that don’t really get disposed but sort of put to sleep?

Thanks in advance.

Edit: Thanks all for all the clever answers. I liked the idea of disposing of a resource’s access instead of the resource itself. Now I’m in search of a better name for the function responsible for the clean up. (Any ideas other than Release() or Sleep()? Thanks again.

  • 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. Editorial Team
    Editorial Team
    2026-05-13T21:31:36+00:00Added an answer on May 13, 2026 at 9:31 pm

    @Jon Skeet has answered the question really well, but let me chip in with a comment that I feel should be an answer by its own.

    It is quite common to use the using code block to temporarily acquire some resource, or enter some form of scoped code you want a clean exit from. I do that all the time, in particular in my business logic controllers, where I have a system that postpones change-events until a block of code has executed, to avoid side-effects multiple times, or before I’m ready for them, etc.

    In order to make the code look more obvious to the programmer that uses it, you should look at using a temporary value that you use instead of the object that has the resource, and returning it from a method name that tells the programmer what it is doing, ie. acquiring some resources temporarily.

    Let me show an example.

    Instead of this:

    using (node) { ... }
    

    you do this:

    using (node.ResourceScope()) { ... }
    

    Thus, you’re not actually disposing of anything more than once, since ResourceScope will return a new value you dispose of, and the underlying node will be left as is.

    Here’s an example implementation (unverified, typing from memory):

    public class Node
    {
        private Resource _Resource;
    
        public void AcquireResource()
        {
            if (_Resource == null)
                _Resource = InternalAcquireResource();
        }
    
        public void ReleaseResource()
        {
            if (_Resource != null)
            {
                InternalReleaseResource();
                _Resource = null;
            }
        }
    
        public ResourceScopeValue ResourceScope()
        {
            if (_Resource == null)
                return new ResourceScopeValue(this);
            else
                return new ResourceScopeValue(null);
        }
    
        public struct ResourceScopeValue : IDisposable
        {
            private Node _Node;
    
            internal ResourceScopeValue(Node node)
            {
                _Node = node;
                if (node != null)
                    node.AcquireResource();
            }
    
            public void Dispose()
            {
                Node node = _Node;
                _Node = null;
                if (node != null)
                    node.ReleaseResource();
            }
        }
    }
    

    This allows you to do this:

    Node node = ...
    using (node.ResourceScope())     // first call, acquire resource
    {
        CallSomeMethod(node);
    }                                // and release it here
    
    ...
    private void CallSomeMethod(Node node)
    {
        using (node.ResourceScope()) // due to the code, resources will not be 2x acquired
        {
        }                            // nor released here
    }
    

    The fact that I return a struct, and not IDisposable means that you won’t get boxing overhead, instead the public .Dispose method will just be called on exit from the using-block.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.