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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:17:09+00:00 2026-05-26T21:17:09+00:00

I am working on a system that uses IntPtr’s returned from two separate C++

  • 0

I am working on a system that uses IntPtr’s returned from two separate C++ DLLs. The unmanaged memory is freed by calling the OBJECT_FREE method presented in each DLL.

Unfortunately, some of the code loses track of which DLL originally allocated the memory. This results in an access violation since we have moved to Win7 (for some reason we were getting away with it on WinXP).

In the medium term, I will have to wrap up every IntPtr to deal with this properly, but in the short term, is there any way to determine which DLL allocated the memory originally?

  • 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-26T21:17:10+00:00Added an answer on May 26, 2026 at 9:17 pm

    No – an IntPtr is just a simple wrapper around an integer (actually a pointer) – there is no additional metadata and its obviously impossible to tell just from a number where it came from.

    If you need to free memory associated with the IntPtr then I recommend that you implement a safe handle for each of your two separate C++ DLLs and modify your PInvoke calls to use safe handles instead, for example:

    internal class FirstDllSafeHandle : SafeHandleZeroOrMinusOneIsInvalid
    {
        private MySafeFileHandle()
            : base(true)
        {
        }
    
        override protected bool ReleaseHandle()
        {        
            return NativeMethods.FirstDll_OBJECT_FREE(handle);
        }
    }
    
    internal class NativeMethods
    {
        [DllImport("whatever.dll")]
        public static extern void FirstDll_OBJECT_FREE(FirstDllSafeHandle handle);
        [DllImport("whatever.dll")]
        public static extern void FirstDll_GetObject(out FirstDllSafeHandle handle);
    
        [DllImport("whatever.dll")]
        public static extern void SecondDll_OBJECT_FREE(SecondDllSafeHandle handle);
        [DllImport("whatever.dll")]
        public static extern void SecondDll_GetObject(out SecondDllSafeHandle handle);
    }
    

    Using safe handles instead of IntPtr offers a number of other advantages as well seamlessly keeping track of the proper way of freeing resources associated with the IntPtr.

    See this MSDN blog article on safe handles to learn more.

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

Sidebar

Related Questions

I'm working with a framework that uses collections derived from System.Collections.CollectionBase . Users have
I'm working on a legacy system that has uses stored procs, business objects and
I'm working on some code that uses the System.Diagnostics.Trace class and I'm wondering how
OK - so am working on a system that uses a custom datepicker control
I'm working on a WordPress site that uses a dynamically generated menu system which
I'm working on a system that uses Jasper reports. Recently it started throwing this
I am currently working on a pyramid system that uses sqlalchemy. This system will
I'm working on a C# project that uses System.Data.OleDb.OleDbCommand class to create and alter
I'm working on a form that uses CA service desk web service request system,
I'm working on an application that uses a list to handle previous guesses from

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.