lua-users home
lua-l archive

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


Hi,

I tested toLua 5 and found a problem. We wanted to use Lua 5 in an embedded  
environment with limited memory. We make extensive use of classes exported to  
Lua as user data. It was no problem to create a pkg file and calling the C++  
class members wasn't a problem at all.

However, the problem I've found was that in the following source, the  
TTestData objects will only be released (deleted from heap memory) when the  
lua_close(L) function is called.

So we ran out of memory when we don't open and close the lua interpreter at  
the beginning and the end of the script execution which is a big perfomance  
problem.

How can I avoid the need to open/close lua?

---------------------------- C++ header ------------------------------
class TTestData {
public:
        unsigned getValue(void) const;
};

class TTest {
public:
        unsigned getCount(void) const;
        TTestData getData(unsigned index) const;

};
---------------------------- C++ header ------------------------------

---------------------------- C++ source ------------------------------
unsigned TTestData::getValue(void) const
{
        return 1234;
}

unsigned TTest::getCount(void) const
{
        return 1234;
}

TTestData TTest::getData(void) const
{
        return TTestData();
}
---------------------------- C++ source ------------------------------

---------------------------- toLua pkg  ------------------------------
class TTestData {
        unsigned getValue(void) const;
};

class TTest {
        unsigned getCount(void) const;
        TTestData getData(unsigned index) const;

};
---------------------------- toLua pkg  ------------------------------

---------------------------- lua script ------------------------------
local count = var:getCount() - 1
for i=0,count do
  local data = var:getData(i);
  print(data.getValue())
end
---------------------------- lua script ------------------------------

Best regards,
Mark Junker