chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Some allocation and object-passing tests


From: Felix Winkelmann
Subject: Re: [Chicken-users] Some allocation and object-passing tests
Date: Wed, 12 Nov 2003 09:40:30 +0100
User-agent: Opera7.21/Win32 M2 build 3218

Am Tue, 11 Nov 2003 11:00:21 +0000 hat Category 5 <address@hidden> geschrieben:

I've written a small test program to try out the various means of
passing Scheme objects safely through a chain of C calls.

Thanks for the code, I was able to reproduce the problem.

#>
#define TRIES  5
#define SCHEME_CALLBACK  scheme_callback1

int dispatcher(void (*fn)(unsigned char *), unsigned char *udata) {
  int i, n = TRIES;
  for (i = 0; i < n; i++)  {
    (*fn)(udata);
    sleep(1);
  }
  return(0);
}

This is the problem: you gc-protect the argument properly, but
inside the dispatcher, `udata' becomes invalid after the first loop
iteration - the gc in the callback changes the address of the value, the gc-protected
pointer is updated, but your local `udata' variable *still points
to the "old" address* ! A solution: protect the
value inside the dispatcher:

int dispatcher(void (*fn)(unsigned char *), unsigned char *udata) {
  int i, n = TRIES;
  C_word *data[ 1 ];
data[ 0 ] = &((C_word)udata); /* casting could be avoided using C_word instead of unsigned char * */
  C_gc_protect(data, 1);
  for (i = 0; i < n; i++)  {
(*fn)(udata); /* location of udata will be updated automatically on each GC */
    sleep(1);
  }
  C_gc_unprotect(data, 1);
  return(0);
}

With these changes, your test-code works fine on my machine.


cheers,
felix




reply via email to

[Prev in Thread] Current Thread [Next in Thread]