-1

I'm using the following function in order to create an UUID and write it in a human-readable form into a pre-allocated buffer. Something goes wrong.

void createUUID(char* pDst)
{
    UUID    lUUIDObj;
    UuidCreate(&lUUIDObj);
    unsigned char*  lDest = reinterpret_cast<unsigned char*>(pDst);
    UuidToStringA(&lUUIDObj, &lDest)
}

At the end of the method, the debugger says:

  • lDest = 0x01fe4fd8 "df4a5ed8-c0d2-495a-84d7-ce0e07cf2113"
  • pDst = 0x0012ec7c "ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ"

I thought that both would have the same content, however it is not the case.

What happened? Thanks.

moala
  • 5,094
  • 9
  • 45
  • 66
  • Are you debugging an optimised build? That will probably not bother to preserve `pDst` across the final function call, since it's not needed after the assignment to `lDest`. What happens if you step out of the function, and look at the result from the caller's viewpoint? – Mike Seymour Jan 17 '12 at 11:25
  • Absolutely not, this is a debug build (/MDd), and once returned, the value in my buffer is also ÌÌÌÌÌÌÌÌÌ... – moala Jan 17 '12 at 11:28

3 Answers3

5

Looking at the documentation for UuidToStringA, it says:

The RPC run-time library allocates memory for the string returned in the StringUuid parameter.

This means that after the call lDest does no longer point to pDst.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1
void createUUID(char* pDst)
{
    UUID    lUUIDObj;
    UuidCreate(&lUUIDObj);
    unsigned char*  lDest = reinterpret_cast<unsigned char*>(pDst);
    //UuidToStringA(&lUUIDObj, &lDest);
    UuidToStringA(&lUUIDObj, lDest);
}

Looks like you clobbered the value of lDest without changing the values it originally pointed to.

spraff
  • 32,570
  • 22
  • 121
  • 229
  • I should have read the pointer value, at least I would have found that it was changing in the UuidToStringA call. – moala Jan 17 '12 at 11:40
0

In order to complete Joachim Pileborg's answer, here is the corrected function:

void createUUID(char* pDst)
{
    UUID    lUUIDObj;
    UuidCreate(&lUUIDObj);
    unsigned char*  lTmpStr;
    UuidToStringA(&lUUIDObj, &lTmpStr);
    sprintf(pDst, reinterpret_cast<char*>(lTmpStr));
    RpcStringFreeA(&lTmpStr);
}
Community
  • 1
  • 1
moala
  • 5,094
  • 9
  • 45
  • 66