Skip to content

Commit cb7e549

Browse files
Add note about when to use HGlobal allocator APIs.
1 parent 912ede2 commit cb7e549

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

xml/System.Runtime.InteropServices/Marshal.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@
303303
<format type="text/markdown"><![CDATA[
304304

305305
## Remarks
306+
> [!IMPORTANT]
307+
> This native memory allocator is a legacy API that should be used exclusively when called for by specific Win32 APIs on the Windows platform. On non-Windows platforms, the <xref:System.Runtime.InteropServices.NativeMemory> class should be used to allocate native memory.
308+
306309
<xref:System.Runtime.InteropServices.Marshal.AllocHGlobal%2A> is one of two memory allocation methods in the <xref:System.Runtime.InteropServices.Marshal> class. (<xref:System.Runtime.InteropServices.Marshal.AllocCoTaskMem%2A?displayProperty=nameWithType> is the other.) This method exposes the Win32 [LocalAlloc](https://go.microsoft.com/fwlink/?LinkId=148628) function from Kernel32.dll.
307310

308311
When <xref:System.Runtime.InteropServices.Marshal.AllocHGlobal%2A> calls `LocalAlloc`, it passes a `LMEM_FIXED` flag, which causes the allocated memory to be locked in place. Also, the allocated memory is not zero-filled.
@@ -383,6 +386,9 @@
383386
<format type="text/markdown"><![CDATA[
384387

385388
## Remarks
389+
> [!IMPORTANT]
390+
> This native memory allocator is a legacy API that should be used exclusively when called for by specific Win32 APIs on the Windows platform. On non-Windows platforms, the <xref:System.Runtime.InteropServices.NativeMemory> class should be used to allocate native memory.
391+
386392
<xref:System.Runtime.InteropServices.Marshal.AllocHGlobal%2A> is one of two memory allocation methods in the <xref:System.Runtime.InteropServices.Marshal> class. (<xref:System.Runtime.InteropServices.Marshal.AllocCoTaskMem%2A?displayProperty=nameWithType> is the other.) This method exposes the Win32 [LocalAlloc](https://go.microsoft.com/fwlink/?LinkID=148628) function from Kernel32.dll.
387393

388394
When <xref:System.Runtime.InteropServices.Marshal.AllocHGlobal%2A> calls `LocalAlloc`, it passes a `LMEM_FIXED` flag, which causes the allocated memory to be locked in place. Also, the allocated memory is not zero-filled.
@@ -2656,6 +2662,9 @@
26562662
<format type="text/markdown"><![CDATA[
26572663

26582664
## Remarks
2665+
> [!IMPORTANT]
2666+
> This native memory allocator is a legacy API that should be used exclusively when called for by specific Win32 APIs on the Windows platform. On non-Windows platforms, the <xref:System.Runtime.InteropServices.NativeMemory> class should be used to allocate native memory.
2667+
26592668
You can use <xref:System.Runtime.InteropServices.Marshal.FreeHGlobal%2A> to free any memory from the global heap allocated by <xref:System.Runtime.InteropServices.Marshal.AllocHGlobal%2A>, <xref:System.Runtime.InteropServices.Marshal.ReAllocHGlobal%2A>, or any equivalent unmanaged API method. If the `hglobal` parameter is <xref:System.IntPtr.Zero?displayProperty=nameWithType> the method does nothing.
26602669

26612670
<xref:System.Runtime.InteropServices.Marshal.FreeHGlobal%2A> exposes the [LocalFree](https://go.microsoft.com/fwlink/?LinkId=148640) function from Kernel32.DLL, which frees all bytes so that you can no longer use the memory pointed to by `hglobal`.
@@ -9569,6 +9578,9 @@ On .NET 6 and later versions, this method is functionally equivalent to <xref:Sy
95699578
<format type="text/markdown"><![CDATA[
95709579

95719580
## Remarks
9581+
> [!IMPORTANT]
9582+
> This native memory allocator is a legacy API that should be used exclusively when called for by specific Win32 APIs on the Windows platform. On non-Windows platforms, the <xref:System.Runtime.InteropServices.NativeMemory> class should be used to allocate native memory.
9583+
95729584
<xref:System.Runtime.InteropServices.Marshal.ReAllocHGlobal%2A> is one of two memory reallocation API methods in the <xref:System.Runtime.InteropServices.Marshal> class. (<xref:System.Runtime.InteropServices.Marshal.ReAllocCoTaskMem%2A?displayProperty=nameWithType> is the other.)
95739585

95749586
This method exposes the Win32 [GlobalReAlloc](https://go.microsoft.com/fwlink/?LinkId=148780) function from Kernel32.dll. The returned pointer can differ from the original. If it is different, the contents of the original memory block have been copied to the new block, and the original memory block has been freed.

0 commit comments

Comments
 (0)