Version française
Home     About     Download     Resources     Contact us    
Browse thread
Help interfacing with C
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Nathaniel Gray <n8gray@g...>
Subject: Re: [Caml-list] Help interfacing with C
On 8/18/06, Damien Doligez <damien.doligez@inria.fr> wrote:
> Hello,
>
> On 2006-08-16, at 21:34, Nathaniel Gray wrote:
>
> > I'm having a heck of a time figuring out why this code *doesn't* work:
>
> Unless you really know what you're doing, you should use the new macros
> (CAMLparam*, CAMLlocal*, CAMLreturn) instead of Begin_roots/End_roots.
> They are slightly less error-prone.

This is a patch to unix.c in the compiler so I'm following the idiom used there.

> > /* fdlist is a list of (file_descr * 'a) pairs.  Filter by FD_ISSET */
> > static value fdset_to_fdlist2(value fdlist, fd_set *fdset)
> > {
> >  value l, p, newres;
> >  value res = Val_int(0);
> >
> >  Begin_roots4(l, p, res, newres);  /* I know, this is aggressive */
> >    for (l = fdlist; l != Val_int(0); l = Field(l, 1)) {
> >      p = Field(l, 0);
> >      int fd = Int_val(Field(p, 0));
> >      if (FD_ISSET(fd, fdset)) {
> >        newres = alloc_small(2, 0);
> >        Field(newres, 0) = p;
> >        Field(newres, 1) = res;
> >        res = newres;
> >      }
> >    }
> >  End_roots();
> >  return res;
> > }
>
> Look at your Begin_roots4.  One of the four parameters is
> uninitialized when you first call alloc_small.  If the minor GC
> is triggered by this allocation, it can result in a segment
> violation or worse.

That's not the problem, but thanks for the tip.  The code crashes
after alloc_small, and quite reliably.

> PS. Maybe there are some other bugs in your code.

You're being too kind.  ;^)  There certainly is at least one other
bug, and I still don't know what it is.  Any other ideas?

Cheers,
-n8

-- 
>>>-- Nathaniel Gray -- Caltech Computer Science ------>
>>>-- Mojave Project -- http://mojave.cs.caltech.edu -->