Forum posts for netmon.exe

[Can't figure out]Memory allocation/freeing error (extremely small code)

While running the code listed at the end, i get a break at the last line FREE(pTcpTable); and if i continue this error shows up:

'Unhandled exception at 0x7737096E (ntdll.dll) in netMon.exe: 0xC0000005: Access violation reading location 0x00000009.'

crt0dat.c opens and this code is shown:

void __cdecl __crtExitProcess (
int status
)
{
__crtCorExitProcess(status);

/*
* Either mscoree.dll isn't loaded,
* or CorExitProcess isn't exported from mscoree.dll,
* or CorExitProcess returned (should never happen).
* Just call ExitProcess.
*/

ExitProcess(status);
}


This is the code:

#include <iostream>
#include <WinSock2.h>
#include <IPHlpApi.h>
#include <Ws2tcpip.h>

using namespace std;

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

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))

int __cdecl main()
{
PMIB_TCPTABLE_OWNER_PID pTcpTable;
DWORD tcpTableSize = 0;

char szLocalAddr[128];

struct in_addr IpAddr;

pTcpTable = (PMIB_TCPTABLE_OWNER_PID)MALLOC(sizeof(MIB_TCPTABLE_OWNER_PID));

if(pTcpTable == NULL)
return 1;

tcpTableSize = sizeof(MIB_TCPTABLE_OWNER_PID);

if(GetExtendedTcpTable(pTcpTable, &tcpTableSize, FALSE, AF_INET, TCP_TABLE_OWNER_PID_CONNECTIONS, 0) == ERROR_INSUFFICIENT_BUFFER)
{
FREE(pTcpTable);

pTcpTable = (PMIB_TCPTABLE_OWNER_PID)MALLOC(sizeof(tcpTableSize));

if(pTcpTable == NULL)
return 1;
}

if(GetExtendedTcpTable(pTcpTable, &tcpTableSize, FALSE, AF_INET, TCP_TABLE_OWNER_PID_CONNECTIONS, 0) == NO_ERROR)
{
for(DWORD i = 0; i < pTcpTable->dwNumEntries; i++)
{
IpAddr.S_un.S_addr = (u_long)pTcpTable->table[i].dwRemoteAddr;

InetNtop(AF_INET, &IpAddr, szLocalAddr, 128);
}
}

if(pTcpTable != NULL)
FREE(pTcpTable);

return 0;
}


I can't figure out whats wrong here, its simple memory allocation and deallocation, kindly help me out here!

View complete forum thread with replies

Other posts related to netmon.exe

See Related Forum Messages: Follow the Links Below to View Complete Thread

[Can&#39;t figure out]Memory allocation/freeing error (extremely small code)
Could not load file or assembly or one of its depencencies. The system cannot find the file &hellip;

What is the carbon footprint of your coffee?

Is it low? Is it high? Can this things really kill the planet Earth? Maybe the answer will surprise you. Maybe not.