diff --git a/src/dmalloc.h b/src/dmalloc.h
index 6fe97b542fce3645902f322f0e14eaa254c3109f..df8af09f0dbd8ad7220f3694e06d784de932df60 100644
--- a/src/dmalloc.h
+++ b/src/dmalloc.h
@@ -1,5 +1,5 @@
 /*
- * $Id: dmalloc.h,v 1.15 1999/08/16 23:54:54 grubba Exp $
+ * $Id: dmalloc.h,v 1.16 1999/10/18 19:15:40 hubbe Exp $
  */
 
 extern char *debug_xalloc(long);
@@ -28,6 +28,11 @@ extern int debug_malloc_register_fd(int, const char *, int);
 extern int debug_malloc_close_fd(int, const char *, int);
 
 void *debug_malloc_update_location(void *,const char *, int);
+
+/* Beware! names of named memory regions are never ever freed!! /Hubbe */
+void *debug_malloc_name(void *p,const char *fn, int line);
+void debug_malloc_copy_names(void *p, void *p2);
+
 #define malloc(x) debug_malloc((x), __FILE__, __LINE__)
 #define calloc(x, y) debug_calloc((x), (y), __FILE__, __LINE__)
 #define realloc(x, y) debug_realloc((x), (y), __FILE__, __LINE__)
@@ -68,4 +73,6 @@ void dmalloc_accept_leak(void *);
 #define dmalloc_register(X,Y,Z,W)
 #define dmalloc_unregister(X,Y)
 #define debug_free(X,Y,Z,Q) free((X))
+#define debug_malloc_name(P,FN,LINE)
+#define debug_malloc_copy_names(p,p2)
 #endif
diff --git a/src/object.c b/src/object.c
index 88d1fc915ade6e0cc43ef62676a22e2106a00537..3e75b75049169cbc2d22eb16f88c5d6ae59c5db8 100644
--- a/src/object.c
+++ b/src/object.c
@@ -5,7 +5,7 @@
 \*/
 /**/
 #include "global.h"
-RCSID("$Id: object.c,v 1.83 1999/09/28 22:00:20 hubbe Exp $");
+RCSID("$Id: object.c,v 1.84 1999/10/18 19:15:41 hubbe Exp $");
 #include "object.h"
 #include "dynamic_buffer.h"
 #include "interpret.h"
@@ -87,6 +87,8 @@ struct object *low_clone(struct program *p)
 
   o=(struct object *)xalloc( ((long)(((struct object *)0)->storage))+p->storage_needed);
 
+  debug_malloc_copy_names(o, p);
+
   o->prog=p;
   add_ref(p);
   o->parent=0;
diff --git a/src/pike_memory.c b/src/pike_memory.c
index 988dc32b5fd20840fa1a303017f36abe701b6ae6..11774ac3ed0623a0c92dfb37bd245469eaad979f 100644
--- a/src/pike_memory.c
+++ b/src/pike_memory.c
@@ -10,7 +10,7 @@
 #include "pike_macros.h"
 #include "gc.h"
 
-RCSID("$Id: pike_memory.c,v 1.43 1999/09/25 20:30:46 grubba Exp $");
+RCSID("$Id: pike_memory.c,v 1.44 1999/10/18 19:15:42 hubbe Exp $");
 
 /* strdup() is used by several modules, so let's provide it */
 #ifndef HAVE_STRDUP
@@ -804,6 +804,33 @@ static int location_number(const char *file, int line)
   return f->number;
 }
 
+static int dynamic_location_number(const char *file, int line)
+{
+  struct fileloc *f,**prev;
+  unsigned long h=hashstr(file, 4711);
+  h*=4711;
+  h+=line;
+  h%=FLSIZE;
+  for(prev=flhash+h;(f=*prev);prev=&f->next)
+  {
+    if(f->line == line && !strcmp(f->file,file))
+    {
+      *prev=f->next;
+      f->next=flhash[h];
+      flhash[h]=f;
+      return f->number;
+    }
+  }
+
+  f=alloc_fileloc();
+  f->line=line;
+  f->file=strdup(file);
+  f->number=~ ( ++file_location_number );
+  f->next=flhash[h];
+  flhash[h]=f;
+  return f->number;
+}
+
 static struct fileloc *find_file_location(int locnum)
 {
   int e;
@@ -956,6 +983,7 @@ static void add_location(struct memhdr *mh, int locnum)
   mlhash[l]=ml;
 }
 
+
 static struct memhdr *low_make_memhdr(void *p, int s, int locnum)
 {
   struct memhdr *mh=get_memhdr(p);
@@ -1161,7 +1189,8 @@ void dump_memhdr_locations(struct memhdr *from,
       continue;
 
     f=find_file_location(l->locnum);
-    fprintf(stderr," *** %s:%d (%d times)\n",f->file,f->line,l->times);
+    fprintf(stderr," *** %s:%d (%d times)%s\n",f->file,f->line,l->times,
+	    l->locnum<0 ? " -" : " ");
   }
 }
 
@@ -1193,11 +1222,13 @@ void list_open_fds(void)
 	  for(l=m->locations;l;l=l->next)
 	  {
 	    struct fileloc *f=find_file_location(l->locnum);
-	    fprintf(stderr,"  *** %s:%d (%d times) %s\n",
+	    fprintf(stderr,"  *** %s:%d (%d times) %s%s\n",
 		    f->file,
 		    f->line,
 		    l->times,
-		    find_location(&no_leak_memlocs, l->locnum) ? "" : " *");
+		    l->locnum<0 ? "-" : "",
+		    find_location(&no_leak_memlocs, l->locnum) ? "" : "*"
+		    );
 	  }
 	}
       }
@@ -1317,6 +1348,45 @@ void * debug_malloc_update_location(void *p,const char *fn, int line)
   return p;
 }
 
+void * debug_malloc_name(void *p,const char *fn, int line)
+{
+  if(p)
+  {
+    struct memhdr *mh;
+    mt_lock(&debug_malloc_mutex);
+    if((mh=my_find_memhdr(p,0)))
+      add_location(mh, dynamic_location_number(fn,line));
+
+    mt_unlock(&debug_malloc_mutex);
+  }
+  return p;
+}
+
+void debug_malloc_copy_names(void *p, void *p2)
+{
+  if(p)
+  {
+    struct memhdr *mh,*from;
+    mt_lock(&debug_malloc_mutex);
+
+    if((from=my_find_memhdr(p2,0)))
+    {
+      struct memloc *l;
+      for(l=from->locations;l;l=l->next)
+      {
+	struct fileloc *f;
+	if(l->locnum < 0)
+	{
+	  if((mh=my_find_memhdr(p,0)))
+	    add_location(mh, l->locnum);
+	}
+      }
+    }
+
+    mt_unlock(&debug_malloc_mutex);
+  }
+}
+
 int debug_malloc_touch_fd(int fd, const char *fn, int line)
 {
   if(fd==-1) return fd;
diff --git a/src/program.c b/src/program.c
index af88d8b70a099199cd622a2748ec6b97a81c0bb1..14b20a0ec59f2dcea7547766d781db0e563a5371 100644
--- a/src/program.c
+++ b/src/program.c
@@ -5,7 +5,7 @@
 \*/
 /**/
 #include "global.h"
-RCSID("$Id: program.c,v 1.158 1999/10/09 23:29:02 hubbe Exp $");
+RCSID("$Id: program.c,v 1.159 1999/10/18 19:15:44 hubbe Exp $");
 #include "program.h"
 #include "object.h"
 #include "dynamic_buffer.h"
@@ -662,6 +662,7 @@ void debug_start_new_program(PROGRAM_LINE_ARGS)
     struct pike_string *s=make_shared_string(file);
     store_linenumber(line,s);
     free_string(s);
+    debug_malloc_name(new_program, file, line);
   }
 #endif
 }
@@ -2627,7 +2628,10 @@ struct program *compile(struct pike_string *prog)
 
   low_start_new_program(0,0,0);
   if(lex.current_file)
-    store_linenumber(last_pc, lex.current_file);
+  {
+    store_linenumber(lex.current_line, lex.current_file);
+    debug_malloc_name(new_program, lex.current_file->str, lex.current_line);
+  }
   compilation_depth=0;
 
 /*  start_line_numbering(); */