- Detect memory leaks when cmd exits
authorThomas Bluemel <thomas@reactsoft.com>
Wed, 1 Aug 2007 12:34:49 +0000 (12:34 +0000)
committerThomas Bluemel <thomas@reactsoft.com>
Wed, 1 Aug 2007 12:34:49 +0000 (12:34 +0000)
- Use _tmain

svn path=/trunk/; revision=28070

reactos/base/shell/cmd/cmd.c
reactos/base/shell/cmd/cmd.rbuild
reactos/base/shell/cmd/cmddbg.c
reactos/base/shell/cmd/cmddbg.h
reactos/base/shell/cmd/main.c [deleted file]
reactos/base/shell/cmd/misc.c

index c40a7fe..4c5a870 100644 (file)
@@ -1611,7 +1611,7 @@ ShowCommands (VOID)
  *
  */
 static VOID
-Initialize (int argc, TCHAR* argv[])
+Initialize (int argc, const TCHAR* argv[])
 {
        TCHAR commandline[CMDLINE_LENGTH];
        TCHAR ModuleName[_MAX_PATH + 1];
@@ -1673,7 +1673,7 @@ Initialize (int argc, TCHAR* argv[])
        if (argc >= 2 && !_tcsncmp (argv[1], _T("/?"), 2))
        {
                ConOutResPaging(TRUE,STRING_CMD_HELP8);
-               ExitProcess(0);
+               cmd_exit(0);
        }
        SetConsoleMode (hIn, ENABLE_PROCESSED_INPUT);
 
@@ -1720,11 +1720,11 @@ Initialize (int argc, TCHAR* argv[])
                                        }
 
                                        ParseCommandLine(commandline);
-                                       ExitProcess (ProcessInput (TRUE));
+                                       cmd_exit (ProcessInput (TRUE));
                                }
                                else
                                {
-                                       ExitProcess (0);
+                                       cmd_exit (0);
                                }
                        }
                        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 */
        if (IsExistingFile (_T("cmdexit.bat")))
@@ -1836,20 +1836,11 @@ static VOID Cleanup (int argc, TCHAR *argv[])
 /*
  * main function
  */
-#ifdef _UNICODE
-int _main(void)
-#else
-int _main (int argc, char *argv[])
-#endif
+int _tmain (int argc, const TCHAR *argv[])
 {
        TCHAR startPath[MAX_PATH];
        CONSOLE_SCREEN_BUFFER_INFO Info;
        INT nExitCode;
-#ifdef _UNICODE
-       PWCHAR * argv;
-       int argc=0;
-       argv = CommandLineToArgvW(GetCommandLineW(), &argc);
-#endif
 
        GetCurrentDirectory(MAX_PATH,startPath);
        _tchdir(startPath);
@@ -1882,6 +1873,7 @@ int _main (int argc, char *argv[])
        /* do the cleanup */
        Cleanup(argc, argv);
 
+       cmd_exit(nExitCode);
        return(nExitCode);
 }
 
index 156d8d6..2879a46 100644 (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="cmd_base">.</include>
+       <include base="cmd">.</include>
        <define name="__USE_W32API" />
        <define name="ANONYMOUSUNIONS" />
        <define name="_WIN32_WINNT">0x0501</define>
-       <define name="UNICODE" />
-       <define name="_UNICODE" />
        <define name="_DEBUG_MEM" />
+       <library>kernel32</library>
+       <library>advapi32</library>
+       <library>shell32</library>
+       <library>user32</library>
        <pch>precomp.h</pch>
        <compilationunit name="unit.c">
                <file>alias.c</file>
                <file>where.c</file>
                <file>window.c</file>
        </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>
 </module>
\ No newline at end of file
index beccaef..ff196c7 100644 (file)
@@ -13,6 +13,9 @@ typedef struct
     int line;
 } alloc_info, *palloc_info;
 
+static size_t allocations = 0;
+static size_t allocated_memory = 0;
+
 static void *
 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));
     if (newptr != NULL)
+    {
+        allocations++;
+        allocated_memory += size;
         newptr = write_redzone(newptr, size, file, line);
+    }
 
     return newptr;
 }
@@ -100,6 +107,7 @@ cmd_alloc_dbg(size_t size, const char *file, int line)
 void *
 cmd_realloc_dbg(void *ptr, size_t size, const char *file, int line)
 {
+    size_t prev_size;
     void *newptr = 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);
+    prev_size = ((palloc_info)ptr)->size;
     check_redzone(ptr, file, line);
 
     newptr = realloc(ptr, calculate_size_with_redzone(size));
     if (newptr != NULL)
+    {
+        allocated_memory += size - prev_size;
         newptr = write_redzone(newptr, size, file, line);
+    }
 
     return newptr;
 }
@@ -127,6 +139,8 @@ cmd_free_dbg(void *ptr, const char *file, int line)
     {
         ptr = get_base_ptr(ptr);
         check_redzone(ptr, file, line);
+        allocations--;
+        allocated_memory -= ((palloc_info)ptr)->size;
     }
 
     free(ptr);
@@ -145,6 +159,11 @@ cmd_checkbuffer_dbg(void *ptr, const char *file, int line)
 void
 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);
 }
 
index 77810f8..af5bbf6 100644 (file)
@@ -17,6 +17,9 @@ cmd_free_dbg(void *ptr, const char *file, int line);
 void
 cmd_checkbuffer_dbg(void *ptr, const char *file, int line);
 
+void
+cmd_exit(int code);
+
 #else
 
 #define cmd_alloc(size) malloc(size)
diff --git a/reactos/base/shell/cmd/main.c b/reactos/base/shell/cmd/main.c
deleted file mode 100644 (file)
index 9f75432..0000000
+++ /dev/null
@@ -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 */
index 87f62ef..bb93d09 100644 (file)
@@ -204,7 +204,9 @@ BOOL add_entry (LPINT ac, LPTSTR **arg, LPCTSTR entry)
        {
                return FALSE;
        }
+    cmd_checkbuffer(q);
        _tcscpy (q, entry);
+    cmd_checkbuffer(q);
 
        oldarg = *arg;
        *arg = cmd_realloc (oldarg, (*ac + 2) * sizeof (LPTSTR));
@@ -213,11 +215,14 @@ BOOL add_entry (LPINT ac, LPTSTR **arg, LPCTSTR entry)
                *arg = oldarg;
                return FALSE;
        }
-
+cmd_checkbuffer(*arg);
        /* save new entry */
        (*arg)[*ac] = q;
+cmd_checkbuffer(*arg);
        (*arg)[++(*ac)] = NULL;
 
+       cmd_checkbuffer(*arg);
+
        return TRUE;
 }