lua-users home
lua-l archive

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


>       When I use my own allocator with lua 5.1, I found a memory leak.
>       lua call the allocator with all the arguments 0 .
>       My allocator, my_realloc(0,0), will return a unique pointer.
>       maybe lua need it return NULL, because I found lua never free
>       this unique pointer anymore.
> 
>       Is it my allocator's bug?

If my_realloc is based on C99 realloc, then there has been a subtle change
in the semantics from C89. The case my_realloc(0,0) is a singularity and
C99 allows a unique pointer to be returned in this case.

To be on the safe side, you should follow the scheme in l_alloc from lauxlib:

static void *my_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
  if (nsize == 0) {
    my_free(ptr);
    return NULL;
  }
  else
    return my_realloc(ptr, nsize);
}

The code above assumes that my_free can handle NULLs and that my_realloc
of NULL allocates new memory.
See http://www.lua.org/manual/5.1/manual.html#lua_Alloc
--lhf