.NET deletes pinned allocated buffer


I have the following code to allocate buffer

uns16 m_rawBuffer = new uns16[m_rawBufferSize]; pin_ptr<uns16> ptrAcqBuffer = m_rawBuffer;

Although it is pin_ptr from time to time GC modifies ptrAcqBuffer.

<a href="https://msdn.microsoft.com/en-us/library/1dz8byfh.aspx" rel="nofollow">From the document</a> I see


A pinning pointer is an interior pointer that prevents the object pointed to from moving on the garbage-collected heap. That is, the value of a pinning pointer is not changed by the common language runtime. This is required when you pass the address of a managed class to an unmanaged function so that the address will not change unexpectedly during resolution of the unmanaged function call.


It doesn't make sesnse to me... Can someone please explain ? Also because I created m_rawBuffer with "new" do I need to "pin_ptr" ?



The garbage collector moves all of the objects on the managed heap whenever it does a garbage collection. This is part of its normal operation. This is why a 'regular' pointer to a managed object isn't valid, because it can be moved at any time by the garbage collector.

A pin pointer marks the object on the managed heap as "don't move me!", so the pointer remains valid as long as the pin_ptr object exists. You can then pass the pin pointer to methods that expect a regular raw pointer, and the object that it's pointing at won't move until the pin_ptr object is destroyed.

All of this has to do with the managed heap. Assuming the code fragment you showed was C++/CLI code, you're using new to allocate an array on the regular <em>unmanaged</em> heap. Pinning is not necessary, it will not move on its own.

If you had done array<UInt16>^ buffer = gcnew array<UInt16>(m_rawBufferSize);, then you would need the pin.


...from time to time GC modifies ptrAcqBuffer.


I'm not sure what's going on there. I'm not sure what pin_ptr does when you try to give it something that isn't on the managed heap in the first place, so weird behavior is probably to be expected. Since you don't need the pin anyway, I wouldn't worry about it.


  • Spring XML View Resolver Configuration
  • Implementation of RSA without dynamic allocation
  • JavaScript Regex Ignore Case
  • Why does Resharper complain when I compare a double to zero?
  • iOS how to stop view rotate
  • Each simulation of normal distribution is the same (C++)
  • java: console application main thread spawns a key listener thread
  • How can i separate app id and version in ionic framework
  • Fishpig Wordpress Magento Post issue
  • JSLint unused variable error
  • Laravel “envoy run” command not working with ssh key
  • AngularJS User Login to Json file
  • Sending big file using FileInputStream/ObjectOutputStream
  • Number formatting in Oracle using TO_CHAR
  • jquery: Disable/Enable button not working after reset
  • Combine rows with same id and delete duplicated rows
  • What's wrong with my PNG IDAT chunk?
  • Eloquent Javascript: Can't understand how the number value is determined in the sum function
  • How should I structure my query string in MVC htaccess applications?
  • Python: Why am I getting a UnicodeDecodeError?
  • copy smaller array into larger array
  • NextJS auth with an external server
  • Encode string to Base64 in Inno Setup (Unicode Version of Inno Setup)
  • How do I set context and followup event in one intent?
  • android 3G signal strength
  • MySQL - Filter records which date is biggest
  • Working with codeception and laravel
  • Parallelization via JDBC - Pyspark - How does parallelization work using JDBC?
  • Paging Through XML Data Using jQuery and HTML
  • On unchecking a checkbox trigger an event
  • Combine two jagged lists into one
  • Php artisan optimize is failing on production server
  • Homomorphic filtering in OpenCV
  • Validating my form with Jquery
  • How can I filter an array of dictionaries in 'updateSearchResultsForSearchController' to s
  • How to get “crispEdges” for SVG text?
  • JQuery Mobile Ajax Navigation in Single-Page Template
  • How to select multiple items from a List view - JavaFX 8
  • DataTables move rows between tables
  • Why does Rails 3 think xE2x80x89 means â x80 x89