- Detect memory leaks when cmd exits

- Use _tmain

svn path=/trunk/; revision=28070
This commit is contained in:
Thomas Bluemel 2007-08-01 12:34:49 +00:00
parent 9787ba0ce3
commit c8479ce1cb
6 changed files with 41 additions and 60 deletions

View file

@ -1611,7 +1611,7 @@ ShowCommands (VOID)
* *
*/ */
static VOID static VOID
Initialize (int argc, TCHAR* argv[]) Initialize (int argc, const TCHAR* argv[])
{ {
TCHAR commandline[CMDLINE_LENGTH]; TCHAR commandline[CMDLINE_LENGTH];
TCHAR ModuleName[_MAX_PATH + 1]; TCHAR ModuleName[_MAX_PATH + 1];
@ -1673,7 +1673,7 @@ Initialize (int argc, TCHAR* argv[])
if (argc >= 2 && !_tcsncmp (argv[1], _T("/?"), 2)) if (argc >= 2 && !_tcsncmp (argv[1], _T("/?"), 2))
{ {
ConOutResPaging(TRUE,STRING_CMD_HELP8); ConOutResPaging(TRUE,STRING_CMD_HELP8);
ExitProcess(0); cmd_exit(0);
} }
SetConsoleMode (hIn, ENABLE_PROCESSED_INPUT); SetConsoleMode (hIn, ENABLE_PROCESSED_INPUT);
@ -1720,11 +1720,11 @@ Initialize (int argc, TCHAR* argv[])
} }
ParseCommandLine(commandline); ParseCommandLine(commandline);
ExitProcess (ProcessInput (TRUE)); cmd_exit (ProcessInput (TRUE));
} }
else else
{ {
ExitProcess (0); cmd_exit (0);
} }
} }
else if (!_tcsicmp (argv[i], _T("/k"))) else if (!_tcsicmp (argv[i], _T("/k")))
@ -1789,7 +1789,7 @@ Initialize (int argc, TCHAR* argv[])
} }
static VOID Cleanup (int argc, TCHAR *argv[]) static VOID Cleanup (int argc, const TCHAR *argv[])
{ {
/* run cmdexit.bat */ /* run cmdexit.bat */
if (IsExistingFile (_T("cmdexit.bat"))) if (IsExistingFile (_T("cmdexit.bat")))
@ -1836,20 +1836,11 @@ static VOID Cleanup (int argc, TCHAR *argv[])
/* /*
* main function * main function
*/ */
#ifdef _UNICODE int _tmain (int argc, const TCHAR *argv[])
int _main(void)
#else
int _main (int argc, char *argv[])
#endif
{ {
TCHAR startPath[MAX_PATH]; TCHAR startPath[MAX_PATH];
CONSOLE_SCREEN_BUFFER_INFO Info; CONSOLE_SCREEN_BUFFER_INFO Info;
INT nExitCode; INT nExitCode;
#ifdef _UNICODE
PWCHAR * argv;
int argc=0;
argv = CommandLineToArgvW(GetCommandLineW(), &argc);
#endif
GetCurrentDirectory(MAX_PATH,startPath); GetCurrentDirectory(MAX_PATH,startPath);
_tchdir(startPath); _tchdir(startPath);
@ -1882,6 +1873,7 @@ int _main (int argc, char *argv[])
/* do the cleanup */ /* do the cleanup */
Cleanup(argc, argv); Cleanup(argc, argv);
cmd_exit(nExitCode);
return(nExitCode); return(nExitCode);
} }

View file

@ -1,12 +1,14 @@
<module name="cmd_base" type="objectlibrary"> <module name="cmd" type="win32cui" installbase="system32" installname="cmd.exe" unicode="yes">
<include base="ReactOS">include/reactos/wine</include> <include base="ReactOS">include/reactos/wine</include>
<include base="cmd_base">.</include> <include base="cmd">.</include>
<define name="__USE_W32API" /> <define name="__USE_W32API" />
<define name="ANONYMOUSUNIONS" /> <define name="ANONYMOUSUNIONS" />
<define name="_WIN32_WINNT">0x0501</define> <define name="_WIN32_WINNT">0x0501</define>
<define name="UNICODE" />
<define name="_UNICODE" />
<define name="_DEBUG_MEM" /> <define name="_DEBUG_MEM" />
<library>kernel32</library>
<library>advapi32</library>
<library>shell32</library>
<library>user32</library>
<pch>precomp.h</pch> <pch>precomp.h</pch>
<compilationunit name="unit.c"> <compilationunit name="unit.c">
<file>alias.c</file> <file>alias.c</file>
@ -64,20 +66,5 @@
<file>where.c</file> <file>where.c</file>
<file>window.c</file> <file>window.c</file>
</compilationunit> </compilationunit>
</module>
<module name="cmd" type="win32cui" installbase="system32" installname="cmd.exe" >
<include base="ReactOS">include/reactos/wine</include>
<include base="cmd">.</include>
<define name="__USE_W32API" />
<define name="ANONYMOUSUNIONS" />
<define name="_WIN32_WINNT">0x0501</define>
<define name="UNICODE" />
<define name="_UNICODE" />
<library>cmd_base</library>
<library>kernel32</library>
<library>advapi32</library>
<library>shell32</library>
<library>user32</library>
<file>main.c</file>
<file>cmd.rc</file> <file>cmd.rc</file>
</module> </module>

View file

@ -13,6 +13,9 @@ typedef struct
int line; int line;
} alloc_info, *palloc_info; } alloc_info, *palloc_info;
static size_t allocations = 0;
static size_t allocated_memory = 0;
static void * static void *
get_base_ptr(void *ptr) get_base_ptr(void *ptr)
{ {
@ -92,7 +95,11 @@ cmd_alloc_dbg(size_t size, const char *file, int line)
newptr = malloc(calculate_size_with_redzone(size)); newptr = malloc(calculate_size_with_redzone(size));
if (newptr != NULL) if (newptr != NULL)
{
allocations++;
allocated_memory += size;
newptr = write_redzone(newptr, size, file, line); newptr = write_redzone(newptr, size, file, line);
}
return newptr; return newptr;
} }
@ -100,6 +107,7 @@ cmd_alloc_dbg(size_t size, const char *file, int line)
void * void *
cmd_realloc_dbg(void *ptr, size_t size, const char *file, int line) cmd_realloc_dbg(void *ptr, size_t size, const char *file, int line)
{ {
size_t prev_size;
void *newptr = NULL; void *newptr = NULL;
if (ptr == NULL) if (ptr == NULL)
@ -111,11 +119,15 @@ cmd_realloc_dbg(void *ptr, size_t size, const char *file, int line)
} }
ptr = get_base_ptr(ptr); ptr = get_base_ptr(ptr);
prev_size = ((palloc_info)ptr)->size;
check_redzone(ptr, file, line); check_redzone(ptr, file, line);
newptr = realloc(ptr, calculate_size_with_redzone(size)); newptr = realloc(ptr, calculate_size_with_redzone(size));
if (newptr != NULL) if (newptr != NULL)
{
allocated_memory += size - prev_size;
newptr = write_redzone(newptr, size, file, line); newptr = write_redzone(newptr, size, file, line);
}
return newptr; return newptr;
} }
@ -127,6 +139,8 @@ cmd_free_dbg(void *ptr, const char *file, int line)
{ {
ptr = get_base_ptr(ptr); ptr = get_base_ptr(ptr);
check_redzone(ptr, file, line); check_redzone(ptr, file, line);
allocations--;
allocated_memory -= ((palloc_info)ptr)->size;
} }
free(ptr); free(ptr);
@ -145,6 +159,11 @@ cmd_checkbuffer_dbg(void *ptr, const char *file, int line)
void void
cmd_exit(int code) cmd_exit(int code)
{ {
if (allocations != 0 || allocated_memory != 0)
{
DbgPrint("CMD: Leaking %lu bytes of memory in %lu blocks! Exit code: %d\n", allocated_memory, allocations, code);
}
ExitProcess(code); ExitProcess(code);
} }

View file

@ -17,6 +17,9 @@ cmd_free_dbg(void *ptr, const char *file, int line);
void void
cmd_checkbuffer_dbg(void *ptr, const char *file, int line); cmd_checkbuffer_dbg(void *ptr, const char *file, int line);
void
cmd_exit(int code);
#else #else
#define cmd_alloc(size) malloc(size) #define cmd_alloc(size) malloc(size)

View file

@ -1,25 +0,0 @@
#include <precomp.h>
#ifdef _UNICODE
extern int _main (void);
#else
extern int _main (int argc, char *argv[]);
#endif
/*
* main function
*/
#ifdef _UNICODE
int main(void)
#else
int main (int argc, char *argv[])
#endif
{
#ifdef _UNICODE
return _main();
#else
return _main(argc, argv);
#endif
}
/* EOF */

View file

@ -204,7 +204,9 @@ BOOL add_entry (LPINT ac, LPTSTR **arg, LPCTSTR entry)
{ {
return FALSE; return FALSE;
} }
cmd_checkbuffer(q);
_tcscpy (q, entry); _tcscpy (q, entry);
cmd_checkbuffer(q);
oldarg = *arg; oldarg = *arg;
*arg = cmd_realloc (oldarg, (*ac + 2) * sizeof (LPTSTR)); *arg = cmd_realloc (oldarg, (*ac + 2) * sizeof (LPTSTR));
@ -213,11 +215,14 @@ BOOL add_entry (LPINT ac, LPTSTR **arg, LPCTSTR entry)
*arg = oldarg; *arg = oldarg;
return FALSE; return FALSE;
} }
cmd_checkbuffer(*arg);
/* save new entry */ /* save new entry */
(*arg)[*ac] = q; (*arg)[*ac] = q;
cmd_checkbuffer(*arg);
(*arg)[++(*ac)] = NULL; (*arg)[++(*ac)] = NULL;
cmd_checkbuffer(*arg);
return TRUE; return TRUE;
} }