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: Damien Doligez <damien.doligez@i...>
Subject: Re: [Caml-list] Help interfacing with C
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.

> /* 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.

-- Damien

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