lua-users home
lua-l archive

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


Greetings everyone.

I am new to Lua, but quite familiar with many programming languages.
Recently I have added Lua support to a project of mine in C++ with the
intent of reading the configuration files through Lua.

Here are the relevant bits that I am using for this:
---------------------------------------------------------------
// memory allocation function
// Note: dLuaByte is a typedef of char*, so as to distinguish it in
the memory manager
void* lua_allocate(void* ud, void* ptr, dSize osize, dSize nsize) {
    (void)ud;
    // allocate
    if(nsize >0 && osize==0) {
        dLuaByte* lb = dCreate(dLuaByte, nsize);
        printf("Lua Alloc: \t%x(%d)\n", lb, nsize);
        return lb;
    }
    // free
    if(nsize == 0 && osize > 0 && ptr != 0) {
        printf("Lua Free: \t%x(%d)\n", ptr, osize);
        const char* tmp = GetMemoryBlockName(ptr);
        dFree(ptr);
    }
    if(nsize == 0) {
        return 0;
    }
    // realloc
    if(nsize != 0 && osize != 0) {
        dSize copysize = dMin(nsize, osize);
        dLuaByte* data = dCreate(dLuaByte, nsize);
        dMemCpy(data, ptr, copysize);
        printf("Lua Move: \t%x(%d) -> %x(%d)\n", ptr, osize, data, nsize);
        dFree(ptr);
        return data;
    }
    return 0;
}

<snip>

// init lua state
    LuaState = lua_open();
    luaL_openlibs(LuaState);
    lua_setallocf(LuaState, lua_allocate, 0);

<snip>

// close lua
if(LuaState != 0) {
        lua_close(LuaState);
        LuaState=0;
    }

<snip>

// read configuration
    int res = lua_load(LuaState, lua_reader, this, "Configuration");
    if(res != 0) {
        return false;
    }
    //try to exec configuration
    if(lua_pcall(LuaState, 0, 0, 0)) {

<snip>

// lua_reader
// Note: ReadChunk is essentially a char* and READ_CHUNK is the constant 4096
const char* dLuaConfig::lua_read(size_t* size) {
    *size = ReadingFile->Read(ReadChunk, READ_CHUNK);
    if(*size) return (const char*)ReadChunk;
    else {
        //end of file
return 0;
---------------------------------------------------------------
I am using this to read values:
---------------------------------------------------------------
lua_getglobal(LuaState, (const char*)name);
    if(lua_isnoneornil(LuaState, -1)) {
        return dVariant(0);
    }
    // if it succeeded..
    dVariant result;
    if(lua_isnumber(LuaState, -1)) {
        dFloat i = (dFloat)lua_tonumber(LuaState, -1);
        result = i;
    } else if(lua_isstring(LuaState, -1)) {
        dString s = lua_tostring(LuaState, -1);
        result = s;
    } else if(lua_istable(LuaState, -1)) {
    	return dVariant("Array");
    }
    lua_remove(LuaState, -1);
---------------------------------------------------------------
And this is my current configuration file:

LogFile = "Draconis.log"
LogFormatter = "dASCIITextFormatter"
LogRules = { "dTimeStampRule", "dConsoleEchoRule" }
Timer = "dWinTimer"

I am using my own memory management subsystem, controlled with the
dCreate and dFree macros.

After printing out all allocations, moves and frees that Lua requests,
it seems that upon closing Lua asks me to free a lot of memory
locations that haven't been allocated (at least not through
lua_allocate() function).

At first I believed that the addresses being freed are from within the
chunk of input data, but after comparing the addresses that turned out
to be false.

Then I tried using the memory manager's functions to identify the
addresses, but not one of them was known. (Although the memory manager
would only be able to identify the address if it was the starting
address of a memory block and not the addresses within.)

I examined the memory data at and around the addresses and they
contain a lot of 0xab, 0xfe and string literal values 'getupvalue',
'sethook', 'setlocal', etc, which seem to be part of a constant table
in ldblib.c

What could be happening in the background to cause such behaviour?

Many thanks in advance,

Marko