[Gc] Pointer References to GC heap

Hans Oesterholt-Dijkema hdnews at gawab.com
Thu Sep 23 15:12:15 PDT 2004


> No, it will not, because you've already freed the memory.  GC_free
> instantly frees memory the exact same as a call to free() does.  t is
> now referencing an invalid address, same as it would have before.

I changed the free function of glib from GC_free() to nofree()

   static void nofree(void *a) {}

which obviously does not do anything. The following is the output\
of my program.

  bigloo/bin/test
  button ok
  get ok
  set ok
  grab focus ok
  set position ok
  Hi there, this is text??
  button ok
  get ok
  set ok
  grab focus ok
  set position ok
  Hi there, this is text????
  button ok
  get ok
  set ok
  grab focus ok
  set position ok
  Hi there, this is text??????
  button ok
  get ok
  set ok
  grab focus ok
  set position ok
  Hi there, this is text????????
  button ok
  get ok
  set ok
  grab focus ok
  set position ok
 
  button ok
  get ok
  set ok
  grab focus ok
  set position ok
  Hi there, this is text????????????

As you can see, the fifth time the pointer to the string
points to nothing (on my Linux iMac it's always the 5th
and the 22nd time) anymore (the code looks I think, analogue 
to this:

  char *t=gtk_entry_get_text(entry);
  char *s=GC_realloc(t,strlen(t)+3);
  strcat(s,"??")
  gtk_entry_set_text(entry,s);
  printf("%s\n",t);

or the scheme variant:

  (let ((s (gtk-entry-get-text entry)))
    (print "get ok")
    (gtk-entry-set-text entry (string-append s "??"))
    (print "set ok")
    (gtk-widget-grab-focus entry)
    (print "grab focus ok")
    (gtk-entry-set-position entry (string-length s))
    (print "set position ok")
    (print s))

What's happening here? With a GC_free() in place of nofree(),
I get strange characters for the fifth time the text is displayed,
for the nofree() I get "the empty string".

Thanks in advance for your answer,

Hans Oesterholt-Dijkema



More information about the Gc mailing list