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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:18:22+00:00 2026-05-16T00:18:22+00:00

I have a class with a field of Dictionary<object, IntPtr> . I allocate the

  • 0

I have a class with a field of Dictionary<object, IntPtr>.

I allocate the memory dynamically, when user calls some method of my class:

IntPtr somePointer = Marshal.AllocHGlobal(/*some desired size*/);

Then I’m going to use that memory in another thread. Actually, after doing some job that thread frees allocated memory thru Marshal.FreeHGlobal and removes the appropriate key from collection. But there is some probability for this thread to crash, so I’m thinking about proper finalization.

How I can finalize that entire collection (in case when some thread has crashed and my memory still remains allocated)?

My inclination is to change IntPtr to SafeHandle. Will this help?

  • 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-16T00:18:23+00:00Added an answer on May 16, 2026 at 12:18 am

    Writing a wrapper for the IntPtr could certainly work. But it is unnecessary if the memory should stay valid as long as the item is stored in the dictionary. If that’s the case, it is much easier on the client code if you create your own directory, one that automatically frees the memory when an item is removed from the dictionary. No need for the client to call Dispose() for each item, that’s always an advantage.

    To do this, derive your own class from IDictionary<object, IntPtr> and IDisposable. You can simply delegate most of the method calls to a private dictionary. You’ll want a custom Add() to allocate the memory, Delete to release it. And implement Dispose() and the finalizer to clean up. The code is kinda ugly though:

    class MyDictionary : IDictionary<object, IntPtr>, IDisposable {
        private Dictionary<object, IntPtr> impl = new Dictionary<object, IntPtr>();
    
        public void Add(object key) {
            IntPtr mem = Marshal.AllocCoTaskMem(666);  // Something smarter here...
            impl.Add(key, mem);
        }
        public bool Remove(object key) {
            if (!impl.ContainsKey(key)) return false;
            Marshal.FreeCoTaskMem(impl[key]);
            return impl.Remove(key);
        }
        protected void Dispose(bool disposing) {
            foreach (IntPtr mem in impl.Values) Marshal.FreeCoTaskMem(mem);
            if (disposing) impl.Clear();
        }
        public void Dispose() { 
            Dispose(true); 
        }
        ~MyDictionary() { 
            Dispose(false); 
        }
    
        // Boilerplate
        public void Add(object key, IntPtr value) { throw new NotImplementedException(); }
        public void Add(KeyValuePair<object, IntPtr> item) { throw new NotImplementedException(); }
        public bool Remove(KeyValuePair<object, IntPtr> item) { throw new NotImplementedException(); }
        public bool ContainsKey(object key) { return impl.ContainsKey(key); }
        public ICollection<object> Keys { get { return impl.Keys; }}
        public bool TryGetValue(object key, out IntPtr value) { return impl.TryGetValue(key, out value); }
        public ICollection<IntPtr> Values { get {return impl.Values; }}
        public IntPtr this[object key] { get { return impl[key]; } set { impl[key] = value; } }
        public void Clear() { impl.Clear(); }
        public bool Contains(KeyValuePair<object, IntPtr> item) { return impl.Contains(item); }
        public void CopyTo(KeyValuePair<object, IntPtr>[] array, int arrayIndex) { (impl as ICollection<KeyValuePair<object, IntPtr>>).CopyTo(array, arrayIndex); }
        public int Count { get { return impl.Count; }}
        public bool IsReadOnly { get { return (impl as ICollection<KeyValuePair<object, IntPtr>>).IsReadOnly; } }
        public IEnumerator<KeyValuePair<object, IntPtr>> GetEnumerator() { return impl.GetEnumerator(); }
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return (impl as System.Collections.IEnumerable).GetEnumerator(); }
    }
    

    AllocCoTaskMem is the better allocator btw, it doesn’t have the legacy baggage.

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

Sidebar

Related Questions

I have a field object and I create a list of fields: class Field
The current class library I am working on will have a base class (Field)
C# question (.net 3.5). I have a class, ImageData, that has a field ushort[,]
I have an abstract base class with a TcpClient field: public abstract class ControllerBase
I have a very simple class with only one field member (e.g. String). Is
I have an abstract base class and I want to declare a field or
I have got a template class as follows: class MyClass<T> { T field; public
I have a class made up of several fields, and I have several constructors.
i have a class whose equality is based on 2 fields such that if
Let's say you have a class called Customer, which contains the following fields: UserName

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.