Question: Program crashing when freeing allocated memory: "Heap block at X modified at Y past requested size of 21".

Running function getUstr, crashes on free(uStr).
Can someone please help me find where am I using unallocated memory?
Thank you!

```
int HexToUChars(char* hexStr, unsigned char **str){
    int i, n;
    int strLen = strlen(hexStr)/2;

    for (i = 0; i < (int)strLen; i++) {

        sscanf_s(hexStr + 2 * i*sizeof(unsigned char), "%02X", *str+i*sizeof(unsigned char));
    }

    return 0;
}

int getUStr(){
    char *hexStr = "E7CA7905DD060F0E437C885BF13DED9243B1D2BD94CB11223DA71360A8F7D2D4";
    unsigned char *uStr;
    size_t strLen = (size_t)strlen(hexStr) / 2;
    uStr = calloc((strLen + 1), sizeof(unsigned char));
    if (uStr != NULL){
        HexToUChars(hexStr, &uStr);
        free(uStr);//Error : Heap block at X modified at Y past requested size of 21
    }
}

```

Answer: 
Well that blog post you linked to strongly implies that that format of \?\ doesn't work in .NET code as it says that if you want to use that then you'll have to resort to calling the Windows APIs yourself... which it doesn't look like you are doing. 

One workaround that I have used in the past is to use the native Windows API function called DefineDosDevice to assign a drive letter to the furthest folder you can reach before you hit the PathToLongException, then use that drive letter from there on to navigate to further sub folders as you now have a much shorter path (and obviously remove this drive letter when you are done). This works for both local paths and network UNC paths. Here's my DllImport definition for vb.net:

```
<DllImport("kernel32.dll", EntryPoint:="DefineDosDeviceW", SetLastError:=True)> _
Public Shared Function DefineDosDevice(ByVal dwFlags As UInteger, <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpDeviceName As String, <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpTargetPath As String) As <MarshalAsAttribute(UnmanagedType.Bool)> Boolean
End Function

```

and to use it:

```
Public Shared Sub AssignDriveLetterToPath(ByVal DriveLetter As Char, ByVal Path As String)
    If Not ApiDefinitions.DefineDosDevice(0, DriveLetter & ":", Path) Then
        Throw New ComponentModel.Win32Exception
    End If
End Sub

Public Shared Sub RemoveAssignedDriveLetter(ByVal DriveLetter As Char, ByVal Path As String)
    If Not ApiDefinitions.DefineDosDevice(ApiDefinitions.DDD_EXACT_MATCH_ON_REMOVE Or ApiDefinitions.DDD_REMOVE_DEFINITION, DriveLetter & ":", Path) Then
        Throw New ComponentModel.Win32Exception
    End If
End Sub

```
It looks like someone has already done the hard work for you and made a .NET wrapper around the Windows APIs that let you use long paths: <https://gallery.technet.microsoft.com:443/DelimonWin32IO-Library-V40-7ff6b16c>