[Gc] GC as a pointer registry

Boehm, Hans hans.boehm at hp.com
Thu Mar 10 10:22:04 PST 2005


Minor issue:

I don't think you gain anything by performing anything by performing
the second test as well.  It is guaranteed to succeed if the first
one did.

Major issue:

This mechanism only distinguishes memory in the garbage-collected
heap from other memory areas.  Deallocated memory may test as
either valid or invalid; there are no guarantees.  Allocated
GC memory should always test as valid; statically allocated
objects or objects allocated with some other allocator should
always test as invalid.

I think there is fundamentally no way to do what you want to do.
If you deallocate an object x, and the memory at location x
is subsequently reallocated, everything will look as though
x is still valid allocated memory; it is; it's just the wrong
object.  So long as x is not reallocated,
you could distinguish the two by having free write some
magic data into the object.  If you used the debug allocation
stuff, it already does that for you in some form.  But
that's really only a heuristic.

Hans

> -----Original Message-----
> From: gc-bounces at napali.hpl.hp.com 
> [mailto:gc-bounces at napali.hpl.hp.com] On Behalf Of 
> andsebjo at student.matnat.uio.no
> Sent: Thursday, March 10, 2005 8:27 AM
> To: gc at napali.hpl.hp.com
> Subject: [Gc] GC as a pointer registry
> 
> 
> I'am doing some work where I need to verify if a pointer 
> points to a valid object or not. This is thought solved by 
> using a GC to keep track of the objects. Some time ago I 
> posted a request for this, and was recommended to use GC_base 
> and/or GC_find_header. The idea is that the function
> GCpointerCheck(ptr) should return 0 (not valid pointer) when 
> a pointer 'ptr' has been freed.
> 
> By compiling and running the example below GC_base and 
> GC_file_header returns as if the pointer still points to a 
> valid object, even though the memory allocated to the pointer 
> has been freed. Any idea of why this does not work or how 
> this problem can be solved? Thank you in advance for all help.
> 
> My GC version is 6.4, I configured  it using configure with 
> no options and make with no options.
> 
> #include "gc.h"
> /** A function which returns true (1) or false (0) depending 
> on some integrity tests */
> 
> 
> int
> GCpointerCheck (void *ptr)
> {
>   int isvalid = 1;      // assume true to start
> 
>   // Run some checks to see if this pointer is valid
>   if (NULL == ptr)
>       isvalid = 0;      // NULL pointers are never good
>   else if (GC_base (ptr) != ptr)
>       //tests whether you have a pointer to beginning of an object
>       //if not it is not valid
>       isvalid = 0;
>   else if (GC_find_header (ptr) == 0)
>       //distinguish GC_-pointers from other valid pointers
>       //if null
>       isvalid = 0;
>   return isvalid;
> };
> 
> int main ()
> {
>   GC_find_leak = 1;
>   int *p[10];
>   int i;
>   for (i = 0; i < 10; ++i)
>       p[i] = GC_malloc (sizeof (int) + i);
> 
>   for (i = 1; i < 10; ++i)
>     {
>       GC_free (p[i]);
>       if (GCpointerCheck (p[i]) == 1)
>         printf ("p[%i] pointer is a valid GC pointer.\n",i);
>       else
>         printf ("p[%i] pointer is not a valid GC pointer.\n",i);
>     }
>   return 1;
> };
> 
> Thanks,
> Andreas Saebjoernsen
> 
> 
> PS! I am sorry if this message was send twice. It bounced as 
> I did the mistake of sending it from a different account than 
> the one I subscribe from, so I resend it.
> 
> 
> 
> _______________________________________________
> Gc mailing list
> Gc at linux.hpl.hp.com 
> http://www.hpl.hp.com/hosted/linux/mail-archives/gc/
> 


More information about the Gc mailing list