diff --git a/src/interpret.c b/src/interpret.c
index ca5146fb12da2931edd782119798458958919130..9cd579c64ab0fccd55677cf49cbfe66d38a20594 100644
--- a/src/interpret.c
+++ b/src/interpret.c
@@ -4,7 +4,7 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: interpret.c,v 1.77 1998/04/16 01:19:37 hubbe Exp $");
+RCSID("$Id: interpret.c,v 1.78 1998/04/16 21:32:01 hubbe Exp $");
 #include "interpret.h"
 #include "object.h"
 #include "program.h"
@@ -542,7 +542,7 @@ static int eval_instruction(unsigned char *pc)
 	free_program(backlog[backlogp].program);
 
       backlog[backlogp].program=fp->context.prog;
-      fp->context.prog->refs++;
+      add_ref(fp->context.prog);
       backlog[backlogp].instruction=instr;
       backlog[backlogp].arg=0;
       backlog[backlogp].pc=pc;
@@ -625,7 +625,7 @@ static int eval_instruction(unsigned char *pc)
 
       CASE(F_LFUN);
       sp->u.object=fp->current_object;
-      fp->current_object->refs++;
+      add_ref(fp->current_object);
       sp->subtype=GET_ARG()+fp->context.identifier_level;
       sp->type=T_FUNCTION;
       sp++;
@@ -1329,7 +1329,7 @@ static int eval_instruction(unsigned char *pc)
 	sp[-args-1].u.object=fp->current_object;
 	sp[-args-1].subtype=GET_ARG()+fp->context.identifier_level;
 	sp[-args-1].type=T_FUNCTION;
-	fp->current_object->refs++;
+	add_ref(fp->current_object);
 
 	return args+1;
       }
@@ -1789,9 +1789,9 @@ void mega_apply(enum apply_type type, INT32 args, void *arg1, void *arg2)
       new_frame.current_storage = o->storage+new_frame.context.storage_offset;
       new_frame.pc = 0;
       
-      new_frame.current_object->refs++;
-      new_frame.context.prog->refs++;
-      if(new_frame.context.parent) new_frame.context.parent->refs++;
+      add_ref(new_frame.current_object);
+      add_ref(new_frame.context.prog);
+      if(new_frame.context.parent) add_ref(new_frame.context.parent);
       
       if(t_flag)
       {
@@ -1992,8 +1992,8 @@ int apply_low_safe_and_stupid(struct object *o, INT32 offset)
   new_frame.context.parent=0;
   fp = & new_frame;
 
-  new_frame.current_object->refs++;
-  new_frame.context.prog->refs++;
+  add_ref(new_frame.current_object);
+  add_ref(new_frame.context.prog);
 
   if(SETJMP(tmp))
   {
diff --git a/src/modules/files/file.c b/src/modules/files/file.c
index 5efe337978f2462bbfc0a9565e2378df3b0319d5..9c1e4dcda84ff1563d903d344e8d1d52734994e6 100644
--- a/src/modules/files/file.c
+++ b/src/modules/files/file.c
@@ -5,7 +5,7 @@
 \*/
 
 #include "global.h"
-RCSID("$Id: file.c,v 1.88 1998/04/09 21:55:55 hubbe Exp $");
+RCSID("$Id: file.c,v 1.89 1998/04/16 21:39:45 hubbe Exp $");
 #include "fdlib.h"
 #include "interpret.h"
 #include "svalue.h"
@@ -128,7 +128,7 @@ OOBOP( || (query_read_oob_callback((X)->fd)==file_read_oob_callback) ||	\
 #define DEBUG_CHECK_INTERNAL_REFERENCE(X)
 #endif
 #define SET_INTERNAL_REFERENCE(f) \
-  do { CHECK_FILEP(f->myself); if(!(f->flags & FILE_HAS_INTERNAL_REF)) { f->myself->refs++; f->flags|=FILE_HAS_INTERNAL_REF; } }while (0)
+  do { CHECK_FILEP(f->myself); if(!(f->flags & FILE_HAS_INTERNAL_REF)) { add_ref(f->myself); f->flags|=FILE_HAS_INTERNAL_REF; } }while (0)
 
 #define REMOVE_INTERNAL_REFERENCE(f) \
   do { CHECK_FILEP(f->myself); if(f->flags & FILE_HAS_INTERNAL_REF) {  f->flags&=~FILE_HAS_INTERNAL_REF; free_object(f->myself); } }while (0)
@@ -1994,7 +1994,7 @@ static void init_file_lock_key(struct object *o)
   THIS_KEY->f=0;
 #ifdef _REENTRANT
   THIS_KEY->owner=thread_id;
-  thread_id->refs++;
+  add_ref(thread_id);
 #endif
 }
 
diff --git a/src/modules/files/socket.c b/src/modules/files/socket.c
index d28f5de9c9df11afd2c5d26b8e7f1c901b7796c4..7defac8012ddb9d89bc94c22140b9bf6f2759a26 100644
--- a/src/modules/files/socket.c
+++ b/src/modules/files/socket.c
@@ -18,7 +18,7 @@
 #include "file_machine.h"
 #include "file.h"
 
-RCSID("$Id: socket.c,v 1.33 1998/04/12 07:06:06 per Exp $");
+RCSID("$Id: socket.c,v 1.34 1998/04/16 21:39:45 hubbe Exp $");
 
 #ifdef HAVE_SYS_TYPE_H
 #include <sys/types.h>
@@ -163,7 +163,7 @@ static void port_listen_fd(INT32 args)
     set_nonblocking(fd,1);
     if(!IS_ZERO(& THIS->accept_callback))
     {
-      fp->current_object->refs++;
+      add_ref(fp->current_object);
       set_read_callback(fd, port_accept_callback, (void *)THIS);
     }
   }
@@ -250,7 +250,7 @@ static void port_bind(INT32 args)
     assign_svalue(& THIS->accept_callback, sp+1-args);
     if(!IS_ZERO(& THIS->accept_callback))
     {
-      fp->current_object->refs++;
+      add_ref(fp->current_object);
       set_read_callback(fd, port_accept_callback, (void *)THIS);
       set_nonblocking(fd,1);
     }
@@ -289,7 +289,7 @@ static void port_create(INT32 args)
 	  assign_svalue(& THIS->accept_callback, sp+1-args);
 	  if(!IS_ZERO(& THIS->accept_callback))
 	  {
-	    fp->current_object->refs++;
+	    add_ref(fp->current_object);
 	    set_read_callback(THIS->fd, port_accept_callback, (void *)THIS);
 	    set_nonblocking(THIS->fd,1);
 	  }
diff --git a/src/object.c b/src/object.c
index 72534520d2c3c8f338d3d269b56eb4d6f8d7ee80..3359b6862e2c7746627654bdac6e157ffbccff47 100644
--- a/src/object.c
+++ b/src/object.c
@@ -4,7 +4,7 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: object.c,v 1.45 1998/04/14 20:04:39 hubbe Exp $");
+RCSID("$Id: object.c,v 1.46 1998/04/16 21:32:02 hubbe Exp $");
 #include "object.h"
 #include "dynamic_buffer.h"
 #include "interpret.h"
@@ -47,7 +47,7 @@ struct object *low_clone(struct program *p)
   o=(struct object *)xalloc( ((long)(((struct object *)0)->storage))+p->storage_needed);
 
   o->prog=p;
-  p->refs++;
+  add_ref(p);
   o->parent=0;
   o->parent_identifier=0;
   o->next=first_object;
@@ -72,7 +72,7 @@ static void call_c_initializers(struct object *o)
   frame.pc=0;
   fp= & frame;
 
-  frame.current_object->refs++;
+  add_ref(frame.current_object);
 
   /* clear globals and call C initializers */
   for(e=p->num_inherits-1; e>=0; e--)
@@ -80,7 +80,7 @@ static void call_c_initializers(struct object *o)
     int d;
 
     frame.context=p->inherits[e];
-    frame.context.prog->refs++;
+    add_ref(frame.context.prog);
     frame.current_storage=o->storage+frame.context.storage_offset;
 
     for(d=0;d<(int)frame.context.prog->num_identifiers;d++)
@@ -154,7 +154,7 @@ struct object *parent_clone_object(struct program *p,
   SET_ONERROR(tmp, do_free_object, o);
   debug_malloc_touch(o);
   o->parent=parent;
-  parent->refs++;
+  add_ref(parent);
   o->parent_identifier=parent_identifier;
   call_c_initializers(o);
   call_pike_initializers(o,args);
@@ -243,7 +243,7 @@ void destruct(struct object *o)
 
   if(!o || !(p=o->prog)) return; /* Object already destructed */
 
-  o->refs++;
+  add_ref(o);
 
   e=FIND_LFUN(o->prog,LFUN_DESTROY);
   if(e != -1)
@@ -287,7 +287,7 @@ void destruct(struct object *o)
     int d;
 
     frame.context=p->inherits[e];
-    frame.context.prog->refs++;
+    add_ref(frame.context.prog);
     frame.current_storage=o->storage+frame.context.storage_offset;
 
     if(frame.context.prog->exit)
@@ -348,7 +348,7 @@ void destruct_objects_to_destruct(void)
     first_object=o;
     o->prev=0;
 
-    o->refs++; /* Don't free me now! */
+    add_ref(o); /* Don't free me now! */
 
     destruct(o);
 
@@ -373,7 +373,7 @@ void really_free_object(struct object *o)
 {
   if(o->prog && (o->prog->flags & PROGRAM_DESTRUCT_IMMEDIATE))
   {
-    o->refs++;
+    add_ref(o);
     destruct(o);
     if(--o->refs > 0) return;
   }
@@ -430,7 +430,7 @@ void low_object_index_no_free(struct svalue *to,
     to->type=T_FUNCTION;
     to->subtype=f;
     to->u.object=o;
-    o->refs++;
+    add_ref(o);
     break;
 
   case IDENTIFIER_CONSTANT:
@@ -442,7 +442,7 @@ void low_object_index_no_free(struct svalue *to,
 	to->type=T_FUNCTION;
 	to->subtype=f;
 	to->u.object=o;
-	o->refs++;
+	add_ref(o);
       }else{
 	check_destructed(s);
 	assign_svalue_no_free(to, s);
@@ -764,12 +764,12 @@ void verify_all_objects(void)
       frame.pc=0;
       fp= & frame;
 
-      frame.current_object->refs++;
+      add_ref(frame.current_object);
 
       for(e=0;e<(int)o->prog->num_inherits;e++)
       {
 	frame.context=o->prog->inherits[e];
-	frame.context.prog->refs++;
+	add_ref(frame.context.prog);
 	frame.current_storage=o->storage+frame.context.storage_offset;
       }
 
@@ -835,7 +835,7 @@ void cleanup_objects(void)
   struct object *o, *next;
   for(o=first_object;o;o=next)
   {
-    o->refs++;
+    add_ref(o);
     destruct(o);
     next=o->next;
     free_object(o);
@@ -914,7 +914,7 @@ void gc_mark_object_as_referenced(struct object *o)
     struct program *p;
 
     if(!o || !(p=o->prog)) return; /* Object already destructed */
-    o->refs++;
+    add_ref(o);
 
     if(o->parent)
       gc_mark_object_as_referenced(o->parent);
@@ -931,7 +931,7 @@ void gc_mark_object_as_referenced(struct object *o)
       int d;
       
       frame.context=p->inherits[e];
-      frame.context.prog->refs++;
+      add_ref(frame.context.prog);
       frame.current_storage=o->storage+frame.context.storage_offset;
 
       if(frame.context.prog->gc_marked)
@@ -972,7 +972,7 @@ void gc_check_all_objects(void)
     int e;
     struct frame frame;
     struct program *p;
-    o->refs++;
+    add_ref(o);
 
 #ifdef DEBUG
     if(o->parent)
@@ -996,7 +996,7 @@ void gc_check_all_objects(void)
 	int d;
 	
 	frame.context=p->inherits[e];
-	frame.context.prog->refs++;
+	add_ref(frame.context.prog);
 	frame.current_storage=o->storage+frame.context.storage_offset;
 	
 	if(frame.context.prog->gc_check)
@@ -1036,7 +1036,7 @@ void gc_mark_all_objects(void)
   {
     if(gc_is_referenced(o))
     {
-      o->refs++;
+      add_ref(o);
       gc_mark_object_as_referenced(o);
       next=o->next;
       free_object(o);
@@ -1054,7 +1054,7 @@ void gc_free_all_unreferenced_objects(void)
   {
     if(gc_do_free(o))
     {
-      o->refs++;
+      add_ref(o);
       destruct(o);
       next=o->next;
       free_object(o);
diff --git a/src/program.c b/src/program.c
index f4f04f17c746d4f16bcb99b5b34988549cd1a7df..25bd20259283c45b69d4d7dd5809c45a6cb30745 100644
--- a/src/program.c
+++ b/src/program.c
@@ -4,7 +4,7 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: program.c,v 1.82 1998/04/16 01:23:03 hubbe Exp $");
+RCSID("$Id: program.c,v 1.83 1998/04/16 21:32:03 hubbe Exp $");
 #include "program.h"
 #include "object.h"
 #include "dynamic_buffer.h"
@@ -417,7 +417,7 @@ void low_start_new_program(struct program *p,
     if((p->next=first_program)) first_program->prev=p;
     first_program=p;
   }else{
-    p->refs++;
+    add_ref(p);
   }
 
   if(name)
@@ -457,12 +457,12 @@ void low_start_new_program(struct program *p,
   fake_object->parent=0;
   fake_object->parent_identifier=0;
   fake_object->prog=p;
-  p->refs++;
+  add_ref(p);
 
   if(name)
   {
     if((fake_object->parent=previous_program_state->fake_object))
-      fake_object->parent->refs++;
+      add_ref(fake_object->parent);
     fake_object->parent_identifier=id;
   }
 
@@ -536,16 +536,16 @@ void really_free_program(struct program *p)
   for(e=0; e<p->num_constants; e++)
     free_svalue(p->constants+e);
 
-  for(e=1; e<p->num_inherits; e++)
+  for(e=0; e<p->num_inherits; e++)
   {
     if(p->inherits[e].name)
       free_string(p->inherits[e].name);
     if(e)
     {
       free_program(p->inherits[e].prog);
-      if(p->inherits[e].parent)
-	free_object(p->inherits[e].parent);
     }
+    if(p->inherits[e].parent)
+      free_object(p->inherits[e].parent);
   }
 
   if(p->prev)
@@ -816,7 +816,7 @@ struct program *end_first_pass(int finish)
     prog=0;
   }else{
     prog=new_program;
-    prog->refs++;
+    add_ref(prog);
 
 #ifdef DEBUG
     check_program(prog);
@@ -1045,7 +1045,7 @@ void low_inherit(struct program *p,
   for(e=0; e<(int)p->num_inherits; e++)
   {
     inherit=p->inherits[e];
-    inherit.prog->refs++;
+    add_ref(inherit.prog);
     inherit.identifier_level += new_program->num_identifier_references;
     inherit.storage_offset += storage_offset;
     inherit.inherit_level ++;
@@ -1103,7 +1103,7 @@ void low_inherit(struct program *p,
 	inherit.parent_offset=0;
       }
     }
-    if(inherit.parent) inherit.parent->refs++;
+    if(inherit.parent) add_ref(inherit.parent);
 
     if(name)
     {
@@ -2233,7 +2233,7 @@ void gc_free_all_unreferenced_programs(void)
     if(gc_do_free(p))
     {
       int e;
-      p->refs++;
+      add_ref(p);
       free_svalues(p->constants, p->num_constants, -1);
       for(e=0;e<p->num_inherits;e++)
       {
diff --git a/src/svalue.h b/src/svalue.h
index 85a6a015bf977231732827e39293b55497bd9840..c6f71a0cdf15fa6115e00fb1263ae43c2c002d70 100644
--- a/src/svalue.h
+++ b/src/svalue.h
@@ -5,7 +5,7 @@
 \*/
 
 /*
- * $Id: svalue.h,v 1.15 1998/04/08 02:47:28 hubbe Exp $
+ * $Id: svalue.h,v 1.16 1998/04/16 21:32:03 hubbe Exp $
  */
 #ifndef SVALUE_H
 #define SVALUE_H
@@ -180,11 +180,19 @@ do{ \
 #define check_refs(S) if((S)->type < MAX_REF_TYPE && (!(S)->u.refs || (S)->u.refs[0] < 0)) fatal("Svalue to object without references.\n")
 #define check_refs2(S,T) if((T) < MAX_REF_TYPE && (S)->refs && (S)->refs[0] <= 0) fatal("Svalue to object without references.\n")
 
+#ifdef DEBUG_MALLOC
+#define add_ref(X) ((INT32 *)debug_malloc_pass( &((X)->refs)))[0]++
+#else
+#define add_ref(X) (X)->refs++
+#endif
+
+
 #else
 
 #define check_type(T)
 #define check_refs(S)
 #define check_refs2(S,T)
+#define add_ref(X) (X)->refs++
 
 #endif