[Gc] Pointer References to GC heap

Hans Oesterholt-Dijkema hdnews at gawab.com
Thu Sep 23 10:25:22 PDT 2004


L.S.,

I'm confused about the workings of GC_malloc().
Can someone explain to me how it is possible to
detect that no variables are referencing the
GC memory? 

E.g.:

  {void *p=GC_MALLOC(10);
   void *q=GC_MALLOC(20);
   int i;
     for(i=0;i<10;i++) {
       q=p;
       p=GC_REALLOC(p,i*i);
     }
  }

How does de GC collector detect that the pointers p
and q are pointing to or not pointing to allocated
memory? Does GC examine the stack?

An other example: Suppose, glib is working with the
GC_malloc and GC_free functions (which is possible).

   GtkEntry *g=gtk_entry_new();
   gtk_entry_set_text("Hi there");

   {
     gchar *t,*t1;
        t=(char *) gtk_entry_get_text(g); // this function actually
                                          // returns const gchar *!

        t1=(char *) GC_MALLOC(strlen(t)+3);
        strdup(t1,t);
        strcat(t1,"!!");
        gtk_entry_set_text(t1);
        printf("%s\n",t);
   }

Note. gtk_entry_get_text() will internally allocate
a new memory block for the text and store the contents 
of t1 in this memory block *and* it will (GC_free) 
the previous memory block. So, t would be pointng 
to freeed memory.

Will GC detect that t is still referencing the memory and
not clean it up? (my recent experiences indicate different
behaviour). Or must I leave out the GC_free function, so
that the memory of GtkEntry is explicitly left to the GC?

Thanks in advance for your answers,
    
--
Hans Oesterholt-Dijkema



More information about the Gc mailing list