Ver código fonte

Test for memory leaks in unit test

Add a test for issue #92 to the unit test
bel 10 anos atrás
pai
commit
62a46e647f
3 arquivos alterados com 25 adições e 16 exclusões
  1. 1 1
      VS2012/unit_test/unit_test.vcxproj
  2. 12 12
      src/civetweb.c
  3. 12 3
      test/unit_test.c

+ 1 - 1
VS2012/unit_test/unit_test.vcxproj

@@ -52,7 +52,7 @@
       </PrecompiledHeader>
       <WarningLevel>Level3</WarningLevel>
       <Optimization>Disabled</Optimization>
-      <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <PreprocessorDefinitions>MEMORY_DEBUGGING;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <AdditionalIncludeDirectories>$(ProjectDir)..\..\src;$(ProjectDir)..\..\include;$(ProjectDir)..\..\src\third_party\lua-5.2.4\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
     </ClCompile>
     <Link>

+ 12 - 12
src/civetweb.c

@@ -443,8 +443,8 @@ static void DEBUG_TRACE_FUNC(const char *func, unsigned line, const char *fmt, .
 #endif /* DEBUG_TRACE */
 
 #if defined(MEMORY_DEBUGGING)
-static unsigned long blockCount = 0;
-static unsigned long totalMemUsed = 0;
+unsigned long mg_memory_debug_blockCount = 0;
+unsigned long mg_memory_debug_totalMemUsed = 0;
 
 static void * mg_malloc_ex(size_t size, const char * file, unsigned line) {
 
@@ -454,12 +454,12 @@ static void * mg_malloc_ex(size_t size, const char * file, unsigned line) {
 
     if (data) {
         *(size_t*)data = size;
-        totalMemUsed += size;
-        blockCount++;
+        mg_memory_debug_totalMemUsed += size;
+        mg_memory_debug_blockCount++;
         memory = (void *)(((char*)data)+sizeof(size_t));
     }
 
-    sprintf(mallocStr, "MEM: %p %5lu alloc   %7lu %4lu --- %s:%u\n", memory, (unsigned long)size, totalMemUsed, blockCount, file, line);
+    sprintf(mallocStr, "MEM: %p %5lu alloc   %7lu %4lu --- %s:%u\n", memory, (unsigned long)size, mg_memory_debug_totalMemUsed, mg_memory_debug_blockCount, file, line);
 #if defined(_WIN32)
     OutputDebugStringA(mallocStr);
 #else
@@ -485,9 +485,9 @@ static void mg_free_ex(void * memory, const char * file, unsigned line) {
 
     if (memory) {
         size = *(size_t*)data;
-        totalMemUsed -= size;
-        blockCount--;
-        sprintf(mallocStr, "MEM: %p %5lu free    %7lu %4lu --- %s:%u\n", memory, (unsigned long)size, totalMemUsed, blockCount, file, line);
+        mg_memory_debug_totalMemUsed -= size;
+        mg_memory_debug_blockCount--;
+        sprintf(mallocStr, "MEM: %p %5lu free    %7lu %4lu --- %s:%u\n", memory, (unsigned long)size, mg_memory_debug_totalMemUsed, mg_memory_debug_blockCount, file, line);
 #if defined(_WIN32)
         OutputDebugStringA(mallocStr);
 #else
@@ -512,15 +512,15 @@ static void * mg_realloc_ex(void * memory, size_t newsize, const char * file, un
             _realloc = realloc(data, newsize+sizeof(size_t));
             if (_realloc) {
                 data = _realloc;
-                totalMemUsed -= oldsize;
-                sprintf(mallocStr, "MEM: %p %5lu r-free  %7lu %4lu --- %s:%u\n", memory, (unsigned long)oldsize, totalMemUsed, blockCount, file, line);
+                mg_memory_debug_totalMemUsed -= oldsize;
+                sprintf(mallocStr, "MEM: %p %5lu r-free  %7lu %4lu --- %s:%u\n", memory, (unsigned long)oldsize, mg_memory_debug_totalMemUsed, mg_memory_debug_blockCount, file, line);
 #if defined(_WIN32)
                 OutputDebugStringA(mallocStr);
 #else
                 DEBUG_TRACE("%s", mallocStr);
 #endif
-                totalMemUsed += newsize;
-                sprintf(mallocStr, "MEM: %p %5lu r-alloc %7lu %4lu --- %s:%u\n", memory, (unsigned long)newsize, totalMemUsed, blockCount, file, line);
+                mg_memory_debug_totalMemUsed += newsize;
+                sprintf(mallocStr, "MEM: %p %5lu r-alloc %7lu %4lu --- %s:%u\n", memory, (unsigned long)newsize, mg_memory_debug_totalMemUsed, mg_memory_debug_blockCount, file, line);
 #if defined(_WIN32)
                 OutputDebugStringA(mallocStr);
 #else

+ 12 - 3
test/unit_test.c

@@ -911,7 +911,7 @@ static void test_request_handlers(void) {
     char uri[64];
     int i;
     const char *request = "GET /U7 HTTP/1.0\r\n\r\n";
-    
+
     ctx = mg_start(NULL, NULL, OPTIONS);
     ASSERT(ctx != NULL);
 
@@ -935,12 +935,12 @@ static void test_request_handlers(void) {
         sprintf(uri, "/U%u", i);
         mg_set_request_handler(ctx, uri, request_test_handler, (void*)i);
     }
-    
+
     conn = mg_download("localhost", atoi(HTTP_PORT), 0, ebuf, sizeof(ebuf), "%s", request);
     ASSERT((conn) != NULL);
     mg_sleep(1000);
     mg_close_connection(conn);
-    
+
     mg_stop(ctx);
 
 }
@@ -1190,6 +1190,15 @@ int __cdecl main(void) {
     /* test completed */
     mg_free(fetch_data);
 
+#ifdef MEMORY_DEBUGGING
+    {
+    extern unsigned long mg_memory_debug_blockCount;
+    extern unsigned long mg_memory_debug_totalMemUsed;
+
+    printf("MEMORY DEBUGGING: %u %u\n", mg_memory_debug_blockCount, mg_memory_debug_totalMemUsed);
+    }
+#endif
+
     printf("TOTAL TESTS: %d, FAILED: %d\n", s_total_tests, s_failed_tests);
     return s_failed_tests == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }