diff --git a/common/Makefile b/common/Makefile
index 9baaf6c5b70d055d2830faf1f145e2b15d55ac9e..a590616eaab728084925a52de74fd502c8f694d5 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -84,6 +84,9 @@ ifdef MINGW
 else
 	mkdir -p $(INCDIR)
 	cp version.h memory.h platform.h ttcn3float.hh pattern.hh CharCoding.hh JSON_Tokenizer.hh $(INCDIR)
+ifeq ($(DEBUG), yes)
+	cp dbgnew.hh $(INCDIR)
+endif
 ifneq ($(TARGETS), )
   ifeq ($(DEBUG), no)
 	$(STRIP) $(TARGETS)
diff --git a/compiler2/makefile.c b/compiler2/makefile.c
index 96ce8708eabc941863753f0a89218c50f8597e4a..46dd924bbdf2180c1ebbe60541a706fe66aba5c9 100644
--- a/compiler2/makefile.c
+++ b/compiler2/makefile.c
@@ -2114,6 +2114,11 @@ static void print_makefile(struct makefile_struct *makefile)
     fputs("# Flags for the C++ preprocessor (and makedepend as well):\n"
           "CPPFLAGS = -D$(PLATFORM) -I$(TTCN3_DIR)/include", fp);
 
+#ifdef MEMORY_DEBUG
+    // enable debug mode for the generated code, too
+    fputs(" -g -DMEMORY_DEBUG", fp);
+#endif
+
     if (makefile->use_runtime_2) fputs(" -DTITAN_RUNTIME_2", fp);
 
     for (i = 0; i < makefile->nBaseDirs; i++) {
diff --git a/core/TTCN3.hh b/core/TTCN3.hh
index 869d3fa8d8f8948d61f50feae032b68536b8dd8a..80be35350671307905ffe45783fe753dd2a349aa 100644
--- a/core/TTCN3.hh
+++ b/core/TTCN3.hh
@@ -32,6 +32,12 @@
 #ifndef TTCN3_HH
 #define TTCN3_HH
 
+#ifdef MEMORY_DEBUG
+// this is needed so the global debug_new_counter object is destroyed after
+// the global objects in the generated C++ files are destroyed
+#include "dbgnew.hh"
+#endif
+
 #include "version.h"
 #include <cversion.h>