Memory allocation/freeing error (extremely small code)

2

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!

c++
c
winapi
memory-management
malloc
asked on Stack Overflow Apr 11, 2013 by user1831704 • edited Jun 1, 2016 by dandan78

2 Answers

4

I'm pretty sure you don't want this:

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

but instead

    pTcpTable = (PMIB_TCPTABLE_OWNER_PID)MALLOC(tcpTableSize);
    //                                          ^^^^^ sizeof removed.

It almost certainly crashes because you overwrote something else that is necessary for the OS to clean up the heap at exit, and that's why you get the crash of accessing address 9.

answered on Stack Overflow Apr 11, 2013 by Mats Petersson
0

The problem is here

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

You need to allocate tcpTableSize bytes, not sizeof(DWORD)

answered on Stack Overflow Apr 11, 2013 by alexrider

User contributions licensed under CC BY-SA 3.0