> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XMemSetWin32HeapTrackingHooks

> XMemSetWin32HeapTrackingHooks

# XMemSetWin32HeapTrackingHooks

Sets tracking callback functions for memory allocation, reallocation, and deallocation of Win32 heaps.

<a id="syntaxSection" />

## Syntax

```cpp theme={null}
VOID XMemSetWin32HeapTrackingHooks(  
         PHEAP_TRACK_ALLOC_ROUTINE AllocRoutine,  
         PHEAP_TRACK_REALLOC_ROUTINE ReAllocRoutine,  
         PHEAP_TRACK_FREE_ROUTINE FreeRoutine  
)  
```

<a id="parametersSection" />

### Parameters

*AllocRoutine*   \_In\_\
Type: [PHEAP\_TRACK\_ALLOC\_ROUTINE](/reference/system/xmem/functions/pheap_track_alloc_routine)

A pointer to a tracking callback function for memory allocation.

*ReAllocRoutine*   \_In\_\
Type: [PHEAP\_TRACK\_REALLOC\_ROUTINE](/reference/system/xmem/functions/pheap_track_realloc_routine)

A pointer to a tracking callback function for memory reallocation.

*FreeRoutine*   \_In\_\
Type: [PHEAP\_TRACK\_FREE\_ROUTINE](/reference/system/xmem/functions/pheap_track_free_routine)

A pointer to a tracking callback function for memory deallocation.

<a id="retvalSection" />

### Return value

Type: VOID

None.

<a id="remarksSection" />

## Remarks

This function allows you to track Win32 heap memory allocation, reallocation, and deallocation by specifying callback functions that are invoked by the GDK memory manager when those heap memory operations are called. For more information about tracking memory operations, see [Diagnostics and memory tracking](/build/console-features/memory/system-memory-diagnostics).

<Note>The CRT makes several heap allocations during initialization and entry points of DLLs/EXEs linked with the CRT.  Since this initialization is a pre-requisite for execution of any code within these modules, it is not typically possible to call XMemSetWin32HeapTrackingHooks prior to *all* heap allocations occurring.  Callback code should be aware of this behavior and handle it accordingly.</Note>

Before calling this function, you must first implement the following corresponding tracking callback functions:

* To track allocation when `HeapAlloc` is invoked, implement a [PHEAP\_TRACK\_ALLOC\_ROUTINE callback](/reference/system/xmem/functions/pheap_track_alloc_routine) function.
* To track reallocation when `HeapReAlloc` is invoked, implement a [PHEAP\_TRACK\_REALLOC\_ROUTINE callback](/reference/system/xmem/functions/pheap_track_realloc_routine) function.
* To track deallocation when `HeapFree` is invoked, implement a [PHEAP\_TRACK\_FREE\_ROUTINE callback](/reference/system/xmem/functions/pheap_track_free_routine) function.

You only need to implement those tracking callback functions required for your tracking requirements. To stop tracking heap memory operations, call this function and specify `NULL` for the appropriate heap memory operations you want to stop tracking.

The following example defines tracking callback functions, then uses the `XMemSetWin32HeapTrackingHooks` function to set and clear hooks to those tracking callback functions.

```cpp theme={null}
void OnHeapAlloc(HANDLE hHeap, DWORD flags, SIZE_T size, PVOID addr)
{
    DebugPrint("[Heap 0x%p] Alloc %I64d bytes at addr: %p\n",
               hHeap, size, addr);
}

void OnHeapRealloc(HANDLE hHeap, DWORD flags, SIZE_T size, PVOID oldAddr, PVOID newAddr)
{
    DebugPrint("[Heap 0x%p] ReAlloc addr: 0x%p to %I64d bytes (new addr: 0x%p)\n",
               hHeap, oldAddr, size, newAddr);
}

void OnHeapFree(HANDLE hHeap, DWORD flags, PVOID addr)
{
    DebugPrint("[Heap 0x%p] Free addr: 0x%p\n",
               hHeap, addr);
}

// Set Hooks
XMemSetWin32HeapTrackingHooks(OnHeapAlloc, OnHeapReAlloc, OnHeapFree);

// Clear Hooks
XMemSetWin32HeapTrackingHooks(NULL, NULL, NULL);
```

<a id="requirementsSection" />

## Requirements

**Header:** xmem.h

**Library:** xmem.lib

**Supported platforms:** XBOX One family consoles and XBOX Series consoles

<a id="seealsoSection" />

## See also

[Memory](/build/console-features/memory/system-memory-overview)\
[XMem](/reference/system/xmem/xmem_members)


## Related topics

- [PHEAP_TRACK_ALLOC_ROUTINE callback](/reference/system/xmem/functions/pheap_track_alloc_routine.md)
- [PHEAP_TRACK_REALLOC_ROUTINE callback](/reference/system/xmem/functions/pheap_track_realloc_routine.md)
- [PHEAP_TRACK_FREE_ROUTINE callback](/reference/system/xmem/functions/pheap_track_free_routine.md)
- [xCurl overview](/build/console-features/networking/web-requests/intro-xcurl.md)
- [XMemSetOption](/reference/system/xmem/functions/xmemsetoption.md)
