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 585045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:57:43+00:00 2026-05-13T14:57:43+00:00

I am implementing a pipeline pattern, the resource, say an image object, will go

  • 0

I am implementing a pipeline pattern, the resource, say an image object, will go through the pipeline, and i have no idea how many clients are holding the resource, so what’s the better way to dispose the resource?

  • 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-13T14:57:43+00:00Added an answer on May 13, 2026 at 2:57 pm

    The way that springs immediately to mind is to wrap the resource inside some form of reference counting “wrapper” so that you can dispose of it when the reference count has released zero.

    Something like this:

    public class RefCountingWrapper<T> where T:IDisposable, new()
    {
        private int referenceCount = 0;
        private T resource;
    
        public RefCountingWrapper(T item)
        {
            resource = item;
        }
    
        public T Acquire()
        {
            referenceCount++;
            return resource;
        }
    
        public void Release()
        {
            referenceCount--;
            if (referenceCount <= 0)
            {
                resource.Dispose();
            }
        }
    }
    

    Caveats that apply:

    • Anyone that has taken the result of Acquire and holds a reference to it will now hold a reference to something that has been disposed (that said, once they’ve released it, they shouldn’t try and access it!).
    • This is vulnerable to people not matching their Acquire’s and Release’s, thus could cause others to encounter a disposed object inadvertantly. That shouldn’t be a massive issue if you control all the code that utilises the resource/calls Acquire/Release though.
    • You’ll probably want to put some locking round the Acquire/Release methods if there’s even the slightest hint of multi-threaded code going near something wrapped by this.
    • There’s absolutely nothing to stop the code that’s using the Acquired resource from calling .Dispose() on the underlying resource
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Implementing a custom Dependency Property on a Framework Element object causes my Visual Studio
Implementing a simple Login screen using JSF and Spring and Hibernate. I have written
when implementing a recursive function during development, i will use a counter and exit
When implementing the Strategy Pattern, where does one put the code that determines which
Implementing a custom VM and I've come to use registers (these will store pointers
Implementing Document-View pattern I faced the problem: standard generic collections and dictionaries ( List<T>
I'm toying with the idea of implementing a generic Producer/Consumer pair + processing queue
Implementing ArrayAccess Interface in PHP , We can access Object Properties as Array Keys
I implementing OLE DB provider for my custom database. It will be used from
Implementing custom DataAnnotationsModelMetadataProvider in ASP.NET MVC2. Assuming the object that is being rendered looks

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.