This documentation is archived and is not being maintained.

Marshal.FreeCoTaskMem Method

Frees a block of memory allocated by the unmanaged COM task memory allocator with Marshal.AllocCoTaskMem.

[Visual Basic]
Public Shared Sub FreeCoTaskMem( _
   ByVal ptr As IntPtr _
public static void FreeCoTaskMem(
 IntPtr ptr
public: static void FreeCoTaskMem(
 IntPtr ptr
public static function FreeCoTaskMem(
   ptr : IntPtr


The address of the memory to be freed.


Exception Type Condition
ArgumentNullException ptr is a null reference (Nothing in Visual Basic).


You can use FreeCoTaskMem to free any memory allocated by AllocCoTaskMem, ReAllocCoTaskMem, or any equivalent unmanaged API. If the ptr parameter is a null reference (Nothing in Visual Basic), the method does nothing.

FreeCoTaskMem exposes the CoTaskMemFree COM API function, which frees all bytes so that you can no longer use the memory pointed to by the ptr parameter. For additional information about CoTaskMemFree, see the MSDN library.

In addition to FreeCoTaskMem, the Marshal class provides two other memory-deallocation methods: Marshal.DestroyStructure and Marshal.FreeHGlobal.

Note   This method uses SecurityAction.LinkDemand to prevent it from being called from untrusted code; only the immediate caller is required to have SecurityPermissionAttribute.UnmanagedCode permission. If your code can be called from partially trusted code, do not pass user input to Marshal class methods without validation. For important limitations on using the LinkDemand member, see Demand vs. LinkDemand.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

Marshal Class | Marshal Members | System.Runtime.InteropServices Namespace | AllocCoTaskMem | ReAllocCoTaskMem | DestroyStructure | FreeHGlobal