lua-users home
lua-l archive

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


> Yes, I guess this is a bug in the Lua core.

Yes, it is a bug :(


> We need to ensure that the GC methods of the module handles are run
> *last*. Alas, there is no such mechanism (yet).

Yes, there is such mechanism. Lua ensures that GC methods are called in
the reverse order of the corresponding userdata creation. Therefore, as
long as a library was loaded before we start creating its userdata, it
should be unloaded only after all its userdata is finalized. The bug
is that Lua is not preserving that order when it closes its state.

Ed Ferguson found this bug a few weeks ago. Sorry I didn't send it to
the list (I thought it was a very rare situation). The following patch
in function luaC_separateudata (lgc.c) should fix it. (The line numbers
may be wrong, because I have made other changes to that file, but I hope
you can find the correct place.)

  @@ -145,9 +145,15 @@
         lastcollected = &curr->gch.next;
       }
     }
  -  /* insert collected udata with gc event into `tmudata' list */
  -  *lastcollected = G(L)->tmudata;
  -  G(L)->tmudata = collected;
  +  /* insert collected udata with gc event in the end of `tmudata' list */
  +  if (G(L)->tmudata == NULL)  /* list is empty? */
  +    G(L)->tmudata = collected;
  +  else {
  +    curr = G(L)->tmudata;
  +    while (curr->gch.next != NULL)  /* search for the last element */
  +      curr = curr->gch.next;
  +    curr->gch.next = collected;
  +  }
     return deadmem;
   }


-- Roberto