Potential bug causing GetPerTcpConnectionEStats to free user allocated buffer when called?

Freedom Sy 26 Reputation points

I just registered here to ask/report this. This is my code:

for (DWORD i = 0; i < ptTable->dwNumEntries; i++) 
row.dwLocalAddr = ptTable->table[i].dwLocalAddr;
row.dwLocalPort = ptTable->table[i].dwLocalPort;
row.dwRemoteAddr = ptTable->table[i].dwRemoteAddr;
row.dwRemotePort = ptTable->table[i].dwRemotePort;
row.dwState = ptTable->table[i].dwState;
if (row.dwRemoteAddr)
std::unique_ptr<UCHAR> rod = std::unique_ptr<UCHAR>(new UCHAR[rodSize]());
ret = GetPerTcpConnectionEStats
} ((PMIB_TCPROW)&row, TcpConnectionEstatsData, NULL, 0, 0, 0, 0, 0, (PUCHAR)rod.get(), 0, rodSize);

Whenever GetPerTcpConnectionEStats is called, the buffer gets freed (the bytes turn into 0xdd 0xdd...).

Note: The problem does not always occur. Within the same debuging session, it happens about 20 times out 100 "for loop" iterations given that every iteration calls GetPerTcpConnectionEStats twice, and only the first call (the one above) causes it.

Also the return from GetPerTcpConnectionEStats is NO_ERROR in all cases. Is it my code that's causing the problem or GetPerTcpConnectionEStats itself is broken?


Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,066 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Freedom Sy 26 Reputation points

    Not sure why the format is messed up! Sorry, it wasn't like this when I clicked submit.

  2. Freedom Sy 26 Reputation points

    This post is now more like a report, because it seems to work just fine if you don't heap allocate at all. The following code works for me, might not work for everyone though:

                TCP_ESTATS_DATA_ROD_v0 rod = { 0 };
                ret= GetPerTcpConnectionEStats((PMIB_TCPROW)&row, TcpConnectionEstatsData, NULL, 0, 0, 0, 0, 0, (PUCHAR)&rod, 0, sizeof(TCP_ESTATS_DATA_ROD_v0));