Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Memory Leak with IcmpSendEcho?

328 views
Skip to first unread message

Tak Shing

unread,
Apr 25, 2011, 6:50:28 PM4/25/11
to
Comfused... Please help

I am trying to check the network status in the 'thread' with
IcmpSendEcho function.
Here is my sample code;


// PingLeak.cpp
//

#include "stdafx.h"
#include <stdio.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <icmpapi.h>
#include <process.h>
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")

// Thread Stopper
BOOL bStopThread = FALSE;

void SendPing()
{
char SendData[32] = "SendData";
const DWORD dwReplySize = (sizeof (ICMP_ECHO_REPLY)) + sizeof
(SendData);

BYTE* pReplyBuffer = new BYTE[dwReplySize];
memset(pReplyBuffer, 0, dwReplySize);

ULONG ipaddr = inet_addr("127.0.0.1");

HANDLE hIcmpFile = IcmpCreateFile();
DWORD dwReturn = IcmpSendEcho(hIcmpFile, ipaddr, SendData,
sizeof(SendData), NULL, (LPVOID)pReplyBuffer, dwReplySize, 1000);

delete [] pReplyBuffer;
IcmpCloseHandle(hIcmpFile);

return;
}

unsigned __stdcall PingThreadFunc(void* pArgument)
{
while (!bStopThread)
{
SendPing();
printf("SendPing\n");
Sleep(100);
}

_endthreadex(0);
return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwThreadId = 0;
HANDLE hPingThread = (HANDLE)_beginthreadex(NULL, 0, (unsigned int
(__stdcall *)(void *))PingThreadFunc, NULL, 0, (unsigned
int*)&dwThreadId);

Sleep(3*60*60*1000); // wait for 3 hours

bStopThread = true;

if (NULL != hPingThread)
{
DWORD dwWait = WaitForSingleObject(hPingThread, 20000);
if (WAIT_OBJECT_0 == dwWait)
CloseHandle(hPingThread);
}

return 0;
}

------------------------------------------

When running this program, PrivateBytes of this process in taskmanager
increase continuously.
I tested running sample for 2 days, but the memory leaking found.
Strange to say, memory leaking repro in Windows 7 only.

I already know some issue that published by Microsoft,
http://support.microsoft.com/kb/2384321

But a example in above page is different with my sample.

Can anyone help me solve this issue ?

Geoff

unread,
Apr 25, 2011, 10:50:31 PM4/25/11
to
On Mon, 25 Apr 2011 15:50:28 -0700 (PDT), Tak Shing
<rockpo...@gmail.com> wrote:

I rewrote your sample code to test for leaks and dump the results:
// PingLeak.cpp
//

#include <stdio.h>
#include <tchar.h>


#include <winsock2.h>
#include <iphlpapi.h>
#include <icmpapi.h>
#include <process.h>

#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
_CrtMemState startup1; // memory diagnostics

delete [] pReplyBuffer;
IcmpCloseHandle(hIcmpFile);

return;
}

_endthreadex(0);
return 0;
}

_CrtMemCheckpoint(&startup1);

HANDLE hPingThread = (HANDLE)_beginthreadex(NULL, 0,
(unsigned int (__stdcall *)(void *))PingThreadFunc,
NULL, 0, (unsigned int*)&dwThreadId);

Sleep(3*60*60*1000); // wait for 3 hours

bStopThread = true;

if (NULL != hPingThread)
{
DWORD dwWait = WaitForSingleObject(hPingThread, 20000);
if (WAIT_OBJECT_0 == dwWait)
CloseHandle(hPingThread);
}

OutputDebugString (L"DumpAllObjects:\n");
_CrtMemDumpAllObjectsSince(&startup1);

OutputDebugString(L"MemDumpStatistics:\n");
_CrtMemDumpStatistics (&startup1);

OutputDebugString(L"DumpMemoryLeaks:\n");
_CrtDumpMemoryLeaks();

return 0;
}

No leaks found on Win7/64 when running this in the debugger for 3
hours as a Win32 debug build.

>
>
>------------------------------------------
>
>When running this program, PrivateBytes of this process in taskmanager
>increase continuously.

I monitored PrivateBytes in Performance Monitor for the process and it
was flat except for a temporary increase when the process went from 7
threads to 8 and back to 7.

David Schwartz

unread,
Apr 26, 2011, 12:37:08 AM4/26/11
to
On Apr 25, 3:50 pm, Tak Shing <rockports....@gmail.com> wrote:

> I already know some issue that published by Microsoft,http://support.microsoft.com/kb/2384321


>
> But a example in above page is different with my sample.
>
> Can anyone help me solve this issue ?

Did you try the workaround suggested in the article? I'll bet you
dollars to donuts that solves your problem.

DS

Tak Shing

unread,
Apr 26, 2011, 3:12:17 AM4/26/11
to

Geoff, thanks for your confirmation.

My sample code was not appropriate.
When sending ping to localhost, the amount of memory leak is less than
ping to other machine.

I rewrite and re-tested :

// PingLeak.cpp
//

#include "stdafx.h"
#include <stdio.h>


#include <winsock2.h>
#include <iphlpapi.h>
#include <icmpapi.h>
#include <process.h>

#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")

// Thread Stopper
BOOL bStopThread = FALSE;

void SendPing(char* pszAddress)


{
char SendData[32] = "SendData";
const DWORD dwReplySize = (sizeof (ICMP_ECHO_REPLY)) + sizeof
(SendData);

BYTE* pReplyBuffer = new BYTE[dwReplySize];
memset(pReplyBuffer, 0, dwReplySize);

ULONG ipaddr = inet_addr(pszAddress);

HANDLE hIcmpFile = IcmpCreateFile();
DWORD dwReturn = IcmpSendEcho(hIcmpFile, ipaddr, SendData,
sizeof(SendData), NULL, (LPVOID)pReplyBuffer, dwReplySize, 1000);

delete [] pReplyBuffer;
IcmpCloseHandle(hIcmpFile);

return;
}

unsigned __stdcall PingThreadFunc(void* pArgument)
{
DWORD dwCycleCount = 0;
DWORD dwCycle = 0;
UINT uiAddress = 1;
char szAddress[32] = { 0 };

while (!bStopThread)
{
sprintf_s(szAddress, sizeof (szAddress), "192.168.0.%d", uiAddress);
SendPing(szAddress);

Sleep(100);

if (100 == dwCycle)
{
printf("SendPing [%d]\n", ++dwCycleCount);
dwCycle = 0;
}
else
++dwCycle;

uiAddress++;
if (128 < uiAddress)
uiAddress = 1;
}

_endthreadex(0);
return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwThreadId = 0;

HANDLE hPingThread = (HANDLE)_beginthreadex(NULL, 0, (unsigned int
(__stdcall *)(void *))PingThreadFunc, NULL, 0, (unsigned
int*)&dwThreadId);

Sleep(3*60*60*1000); // wait for 3 hours

bStopThread = true;

if (NULL != hPingThread)
{
DWORD dwWait = WaitForSingleObject(hPingThread, 20000);
if (WAIT_OBJECT_0 == dwWait)
CloseHandle(hPingThread);
}

return 0;
}

Sending ping to 192.168.0.1 >>> 192.168.0.128,
the average of privatebytes increment was 12KB per hour.
This is monotonically increasing...


Geoff <ge...@invalid.invalid> wrote:
> On Mon, 25 Apr 2011 15:50:28 -0700 (PDT), Tak Shing
>

Tak Shing

unread,
Apr 26, 2011, 3:23:13 AM4/26/11
to
Thanks for your reply.

> Did you try the workaround suggested in the article? I'll bet you
> dollars to donuts that solves your problem.

Yes, I tried to modified my code, but it was not resolved.
The workaround suggested has the structure that called and make
pinging thread repeatedly.
It is different with my code.
And the average of privatebytes increase is more than 50MB per hour
in the KB.

David Schwartz

unread,
Apr 27, 2011, 5:23:34 PM4/27/11
to
On Apr 26, 12:23 am, Tak Shing <rockports....@gmail.com> wrote:

> Yes, I tried to modified my code, but it was not resolved.
> The workaround suggested has the structure that called and make
> pinging thread repeatedly.

No, the workaround consisted of adding a single line of code before
any threads were created.

> It is different with my code.
> And the average of privatebytes increase is more than 50MB per hour
> in the KB.

Try the workaround included in the article you cited. It sounds like
you didn't actually do that. The workaround is the same regardless of
what code creates the problem.

DS

Tak Shing

unread,
Apr 27, 2011, 6:08:09 PM4/27/11
to
Thanks for your reply.

> Try the workaround included in the article you cited. It sounds like
> you didn't actually do that. The workaround is the same regardless of
> what code creates the problem.

I already tried it, but Nothing has worked.
I put a sigle line before PingThread created.

HANDLE hIcmpFile = IcmpCreateFile();


HANDLE hPingThread = (HANDLE)_beginthreadex(NULL, 0, (unsigned int
(__stdcall *)(void *))PingThreadFunc, NULL, 0, (unsigned
int*)&dwThreadId);

> No, the workaround consisted of adding a single line of code before
> any threads were created.

The diffrence I metioned was that pinging once or repeatedly in a
thread.

And I also tried to change handle creation.
(1) call IcmpCreateFile/IcmpCloseHandle at every pinging
(2) call IcmpCreateFile/IcmpCloseHandle at one time with loop

And I also tried to change buffer size.

But but Nothing has worked...

0 new messages