Josh
Josh

Reputation: 6282

Application freezes when freeing memory

I have the following code where I try to convert the string 03080000 into a byte array with the first byte being 03, second being 08, third 00, and fourth 00.

But it keeps freezing when i'm freeing the memory.
I tried stepping through it with a debugger (visual studio), but when I step over the free() function the debugger just seems to stop and it just hangs.

Am I corrupting bData by the way I'm writing to it? Or what could be wrong?

LPCWSTR lpValueData = L"03080000"
WCHAR HexChar[2] = {0};

UINT i;
UINT n = 0;

DWORD dwDataSize;
PBYTE bData;

dwDataSize = wcslen(lpValueData) / 2;
bData = (PBYTE) malloc(dwDataSize);
for (i = 0; i < dwDataSize * 2; i += 2)
{
    HexChar[0] = lpValueData[i];
    HexChar[1] = lpValueData[i + 1];

    swscanf_s(HexChar, L"%X", &bData[n++]);
}
// I want bData to be {0x03, 0x08, 0x00, 0x00}
// Compare bData to another byte array here with memcmp
free(bData); // freezes here.

Upvotes: 0

Views: 267

Answers (1)

Maximus
Maximus

Reputation: 10855

L"%X" requested pointer to int, rather BYTE

So, it must be

int x;
swscanf_s(HexChar, L"%X", &x);
bData[n++] = x;

Upvotes: 1

Related Questions