[ros-diffs] [weiden] 28070: - Detect memory leaks when cmd exits - Use _tmain

weiden at svn.reactos.org weiden at svn.reactos.org
Wed Aug 1 14:34:50 CEST 2007


Author: weiden
Date: Wed Aug  1 16:34:49 2007
New Revision: 28070

URL: http://svn.reactos.org/svn/reactos?rev=28070&view=rev
Log:
- Detect memory leaks when cmd exits
- Use _tmain

Removed:
    trunk/reactos/base/shell/cmd/main.c
Modified:
    trunk/reactos/base/shell/cmd/cmd.c
    trunk/reactos/base/shell/cmd/cmd.rbuild
    trunk/reactos/base/shell/cmd/cmddbg.c
    trunk/reactos/base/shell/cmd/cmddbg.h
    trunk/reactos/base/shell/cmd/misc.c

Modified: trunk/reactos/base/shell/cmd/cmd.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/shell/cmd/cmd.c?rev=28070&r1=28069&r2=28070&view=diff
==============================================================================
--- trunk/reactos/base/shell/cmd/cmd.c (original)
+++ trunk/reactos/base/shell/cmd/cmd.c Wed Aug  1 16:34:49 2007
@@ -1611,7 +1611,7 @@
  *
  */
 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 @@
 	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 @@
 					}
 
 					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 @@
 }
 
 
-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 @@
 /*
  * 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 @@
 	/* do the cleanup */
 	Cleanup(argc, argv);
 
+	cmd_exit(nExitCode);
 	return(nExitCode);
 }
 

Modified: trunk/reactos/base/shell/cmd/cmd.rbuild
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/shell/cmd/cmd.rbuild?rev=28070&r1=28069&r2=28070&view=diff
==============================================================================
--- trunk/reactos/base/shell/cmd/cmd.rbuild (original)
+++ trunk/reactos/base/shell/cmd/cmd.rbuild Wed Aug  1 16:34:49 2007
@@ -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>
@@ -64,20 +66,5 @@
 		<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>

Modified: trunk/reactos/base/shell/cmd/cmddbg.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/shell/cmd/cmddbg.c?rev=28070&r1=28069&r2=28070&view=diff
==============================================================================
--- trunk/reactos/base/shell/cmd/cmddbg.c (original)
+++ trunk/reactos/base/shell/cmd/cmddbg.c Wed Aug  1 16:34:49 2007
@@ -12,6 +12,9 @@
     const char *file;
     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 @@
 
     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 @@
 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 @@
     }
 
     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 @@
     {
         ptr = get_base_ptr(ptr);
         check_redzone(ptr, file, line);
+        allocations--;
+        allocated_memory -= ((palloc_info)ptr)->size;
     }
 
     free(ptr);
@@ -145,6 +159,11 @@
 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);
 }
 

Modified: trunk/reactos/base/shell/cmd/cmddbg.h
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/shell/cmd/cmddbg.h?rev=28070&r1=28069&r2=28070&view=diff
==============================================================================
--- trunk/reactos/base/shell/cmd/cmddbg.h (original)
+++ trunk/reactos/base/shell/cmd/cmddbg.h Wed Aug  1 16:34:49 2007
@@ -17,6 +17,9 @@
 void
 cmd_checkbuffer_dbg(void *ptr, const char *file, int line);
 
+void
+cmd_exit(int code);
+
 #else
 
 #define cmd_alloc(size) malloc(size)

Removed: trunk/reactos/base/shell/cmd/main.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/shell/cmd/main.c?rev=28069&view=auto
==============================================================================
--- trunk/reactos/base/shell/cmd/main.c (original)
+++ trunk/reactos/base/shell/cmd/main.c (removed)
@@ -1,25 +1,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 */

Modified: trunk/reactos/base/shell/cmd/misc.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/shell/cmd/misc.c?rev=28070&r1=28069&r2=28070&view=diff
==============================================================================
--- trunk/reactos/base/shell/cmd/misc.c (original)
+++ trunk/reactos/base/shell/cmd/misc.c Wed Aug  1 16:34:49 2007
@@ -204,7 +204,9 @@
 	{
 		return FALSE;
 	}
+    cmd_checkbuffer(q);
 	_tcscpy (q, entry);
+    cmd_checkbuffer(q);
 
 	oldarg = *arg;
 	*arg = cmd_realloc (oldarg, (*ac + 2) * sizeof (LPTSTR));
@@ -213,10 +215,13 @@
 		*arg = oldarg;
 		return FALSE;
 	}
-
+cmd_checkbuffer(*arg);
 	/* save new entry */
 	(*arg)[*ac] = q;
+cmd_checkbuffer(*arg);
 	(*arg)[++(*ac)] = NULL;
+
+	cmd_checkbuffer(*arg);
 
 	return TRUE;
 }




More information about the Ros-diffs mailing list