Commit c421b7010d915a36bfc90c4f0462095ea840bb7a

Timothy Gu 2015-03-15T16:09:44

Check for memory leak when _DEBUG is defined

diff --git a/dlfcn.c b/dlfcn.c
index 75f5c81..add1f4f 100644
--- a/dlfcn.c
+++ b/dlfcn.c
@@ -17,6 +17,11 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#ifdef _DEBUG
+#define _CRTDBG_MAP_ALLOC
+#include <stdlib.h>
+#include <crtdbg.h>
+#endif
 #include <windows.h>
 #include <stdio.h>
 
diff --git a/test.c b/test.c
index 4dff8f0..d6280e4 100644
--- a/test.c
+++ b/test.c
@@ -18,6 +18,11 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+#ifdef _DEBUG
+#define _CRTDBG_MAP_ALLOC
+#include <stdlib.h>
+#include <crtdbg.h>
+#endif
 #include <stdio.h>
 #include "dlfcn.h"
 
@@ -71,6 +76,15 @@ int main()
     int (*nonexistentfunction)( void );
     int ret;
 
+#ifdef _DEBUG
+    _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
+    _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
+    _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
+    _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);
+    _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
+    _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);
+#endif
+
     library = dlopen( "testdll.dll", RTLD_GLOBAL );
     if( !library )
     {
@@ -295,5 +309,8 @@ int main()
     else
         printf( "SUCCESS\tClosed global handle.\n" );
 
+#ifdef _DEBUG
+    _CrtDumpMemoryLeaks();
+#endif
     return 0;
 }
diff --git a/testdll.c b/testdll.c
index 2c40014..ff99f87 100644
--- a/testdll.c
+++ b/testdll.c
@@ -17,6 +17,11 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+#ifdef _DEBUG
+#define _CRTDBG_MAP_ALLOC
+#include <stdlib.h>
+#include <crtdbg.h>
+#endif
 #include <stdio.h>
 
 #if defined(_WIN32)