有谁能帮我看看为什么在释放内存的时候会报错?五一快乐,40分相赠!
/***********HEAD FILES*******************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
/***********GLOBAL MACROS***************/
#define DEBUG
#define GLOBAL
#define CALLBACK
#define FALSE 0
#define TRUE 1
#define MAX_CMD_COUNT 100
#define DEFAULT_CMD_COUNT 1
#define NULL 0
#define HEAP_COUNT 200
/***********GLOBAL TYPEDEFINE***************/
typedef int BOOL;
typedef void (*pRegCommand) (void) ;
typedef struct
{
char* CmdString;
pRegCommand CmdEntry;
}CmdStructure;
typedef struct
{
void* HeapPtr;
int iLineNum;
int iSize;
BOOL bUse;
}ArrHeapDump;
/***********GLOBAL VARIABLE***************/
GLOBAL char strCmd[10];
GLOBAL char* strPrompt="SocketUDP>>";
GLOBAL BOOL bSystemExit=FALSE;
GLOBAL int iCurrentHeapCount=0;
GLOBAL int CurrentCmdNum=1;
GLOBAL ArrHeapDump arrHeapCount[HEAP_COUNT];
GLOBAL CmdStructure* arrCmdList[MAX_CMD_COUNT];
DEBUG void DEBUG_MESSAGE(void * msg)
{
printf("DEBUG_MESSAGE:%08x\n",msg);
}
DEBUG void DEBUG_MemoryDump()
{
int ix;
int nCount=0;
printf("MemDump:\n");
for(ix=0;ix<iCurrentHeapCount;ix++)
{
if(arrHeapCount[iCurrentHeapCount].bUse==TRUE)
{
printf("LINE:%d\tADDRESS:%08x\tSIZE:%08x\n",
arrHeapCount[iCurrentHeapCount].iLineNum,
arrHeapCount[iCurrentHeapCount].HeapPtr,
arrHeapCount[iCurrentHeapCount].iSize);
nCount++;
}
}
printf("Dump Item=%d\n",nCount);
}
GLOBAL void * Malloc_Dump(int iSize,int LineNum)
{
void *p;
iCurrentHeapCount++;
p=malloc(iSize);
assert(p!=0);
arrHeapCount[iCurrentHeapCount-1].HeapPtr=p;
arrHeapCount[iCurrentHeapCount-1].iLineNum=LineNum;
arrHeapCount[iCurrentHeapCount-1].iSize=iSize;
arrHeapCount[iCurrentHeapCount-1].bUse=TRUE;
return p;
}
GLOBAL void Free_Dump(void* p)
{
free((void*)p);
arrHeapCount[iCurrentHeapCount].bUse=FALSE;
arrHeapCount[iCurrentHeapCount].HeapPtr=NULL;
iCurrentHeapCount--;
}
GLOBAL void System_Exit()
{
bSystemExit=TRUE;
}
GLOBAL BOOL RUTIME_InitCommand()
{
BOOL bRet;
if(arrCmdList[CurrentCmdNum-1]!=NULL)
{
arrCmdList[CurrentCmdNum-1]->CmdEntry=System_Exit;
arrCmdList[CurrentCmdNum-1]->CmdString="Exit";
bRet=TRUE;
}
else
{
bRet=FALSE;
}
return bRet;
}
GLOBAL BOOL RUNTIME_RegistCommand(void* cmdEntry,char *cmdName)
{
assert(cmdName!=NULL);
CurrentCmdNum++;
arrCmdList[CurrentCmdNum-1]=(CmdStructure*)Malloc_Dump(sizeof(arrCmdList[0]),__LINE__);
arrCmdList[CurrentCmdNum-1]->CmdEntry=(pRegCommand)cmdEntry;
arrCmdList[CurrentCmdNum-1]->CmdString=cmdName;
return TRUE;
}
GLOBAL void RUNTIME_InitSystem()
{
int ix;
CurrentCmdNum=DEFAULT_CMD_COUNT;
for(ix=0;ix<CurrentCmdNum;ix++)
{
arrCmdList[ix]=NULL;
}
for(ix=0;ix<CurrentCmdNum;ix++)
{
DEBUG_MESSAGE((void *)arrCmdList[ix]);
arrCmdList[ix]=(CmdStructure* )Malloc_Dump(sizeof(arrCmdList[ix]),__LINE__);
DEBUG_MESSAGE((void *)arrCmdList[ix]);
}
RUTIME_InitCommand();
}
GLOBAL void TermSystem()
{
int ix;
for(ix=0;ix<CurrentCmdNum;ix++)
{
Free_Dump((void*)arrCmdList[ix]);
arrCmdList[ix]=NULL;
CurrentCmdNum=1;
}
}
void test()
{
printf("Hello");
}
int main()
{
RUNTIME_InitSystem();
RUNTIME_RegistCommand((void*)test,"test");
while(1)
{
printf(strPrompt);
gets(strCmd);
printf("\n");
switch(strCmd[0])
{
case 'e':
{
printf("Exit System.\n");
bSystemExit=TRUE;
break;
}
default:
break;
}
if(bSystemExit)
{
break;
}
}
TermSystem();
DEBUG_MemoryDump();
return 0;
}