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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:31:36+00:00 2026-06-10T06:31:36+00:00

I have a couple of unmanaged memory structures used to communicate with c++ dlls.

  • 0

I have a couple of unmanaged memory structures used to communicate with c++ dlls.
Each such structure has to be freed manually, so I wrap it in a MyUnmanagedStructure which implements IDisposable.

I always need a variable number of these structures together, so I have a collection MyUnmanagedStructureCollection which also implements IDisposable.

(see below for the minimal example code)

As long as the user of my library always calls Dispose() or wraps the collection with using() {} there is no problem, but I cannot assure that. I do not want to leak memory even if the user does not dispose the collection manually.

When the MyUnmanagedStructureCollection.Dispose() method is called by the garbage collection via finalizer instead, then as far as I understand I cannot be sure that my private List<MyUnmanagedStructure> has not been garbage collected already, so how can I dispose of each structure in that case?

In my finalizing code, should I attempt to iterate over the list, hoping that it has not been garbage collected yet?

Is it good practise to do this in a try/catch block, catching ObjectDisposedException?

Or should I let each unmanagedStructure “fend for itself”, relying on the individual finalizers, and simply do nothing in the finalizer of my collection?

public class MyUnmanagedStructureCollection : IDisposable
{
    private List<MyUnmanagedStructure> structures;
    private bool disposed = false;

    #region standard IDIsposable pattern
    public ~MyUnmanagedStructureCollection()
    {
        this.Dispose(false);
    }

    public void Dispose()
    {
        this.Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!disposed)
        {
            // Dispose unmanaged resources 
            // Should not access managed resources, 
            // the garbage collection may have claimed them already!

            // PROBLEM!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            // this.structures is a List<MyUnmanagedStructure>; so is a managed resource!!!

            foreach (var structure in this.structures)
                 structure.Dispose(disposing)
            this.removeAllMemoryPressure();

            if (disposing) 
            {
                // Dispose managed resources.
                this.structures.Clear();
                this.structures = null;
            }

        }
        disposed = true;
    }
}


public class MyUnmanagedBuffer : IDisposable
{
...
}
  • 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-06-10T06:31:37+00:00Added an answer on June 10, 2026 at 6:31 am

    The way the GC works is:

    1. Find all reachable objects
    2. Enqueue all unreachable objects with a finalizer into the finalization queue
    3. Mark all objects that are reachable from from the finalization queue as reachable
    4. Free the remaining unreachable objects

    An object that’s referenced from an object whose finalizer is running cannot have been garbage collected yet.

    The only thing you need to be careful about is that the order of finalization is undefined. So the elements of the list might have been finalized yet, but not collected. Finalization is guaranteed to be single threaded, so you need locking too.

    One generally tries to avoid such finalization chains, since independent finalization is simpler. But if some objects need to be disposed before others, such a construction is unavoidable.

    You should also consider critical finalization using SafeHandles.


    Reachability

    One of the guidelines for finalization is that a Finalize method shouldn’t touch other objects. People sometimes incorrectly assume that this is because those other objects have already been collected. Yet, as I have explained, the entire reachable graph from a finalizable object is promoted.

    The real reason for the guideline is to avoid touching objects that may have already been finalized. That’s because finalization is unordered.

    Chris Brumme on finalization

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

Sidebar

Related Questions

I have couple resource DLLs that I currently load when application starts using following
I have couple of questions: What is exactly VJoyD used for? Based on my
I have couple of rows in database table (lets call it Customer), each row
I have couple of tags which has same name attribute. Something like this: <img
I have a couple specific questions regarding mixing managed C++ with unmanaged C++: If
I have xib in which i have couple of views, but basically structure looks
I have couple of hyperlinks on my website. What I want is to remove
I have couple of dozen pieces of data that I need to save and
I have couple of formulas and data coming from database. I want to refresh
I have couple questions regarding some C++ rules. Why am I able to call

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.