The only possible reasons that come to my mind are when
size
is
UINT_MAX
(0xFFFFFFFF) or reading the string length is interrupted by a write operation completion on the write end of the pipe.
To avoid the latter, use a loop for reading:
bool read(HANDLE hPipe, TYPE &data)
{
DWORD size = sizeof(TYPE);
bool ret = true;
char *buf = reinterpret_cast<char*>(&data);
do
{
DWORD dwBytesRead = 0;
ret = ReadFile(hPipe, buf, size, &dwBytesRead, NULL);
buf += dwBytesRead;
size -= dwBytesRead;
} while (ret && size);
return ret;
}
Additional notes:
Why do you cast here?
bool ret=read(hPipe,(int &)size);
When casting is really necessary (which it isn't here), use C++ casts and not C casts.
You forgot to delete
c
so that you have a memory leak.