tolua bug / limitation

January 2nd, 2009 by Dan East

Minor update: I now use swig for my C++ / Lua binding, and I like it much better.

I’ve been using the Lua programming language for several years now as an embedded scripting language in a game engine.  In order to bind C++ classes into the Lua environment I use a 3rd-party library called tolua.

Recently I kept running into a bug with a new class interfaced via tolua that would manifest differently between Release and Debug builds.  The symptom was apparent corruption of other unrelated objects within Lua.  The specific situation was accessing a member of a class that was also a class.  Here’s an example to make things clearer:

class B {
int n;
};
class A {
B b;
};
//create an object a of type A
A a;

Then in lua when accessing the nested class the bug would be triggered:

a.b.n=5

tolua passes instances of C++ classes into lua using simple user data, which is basically just a pointer.  To reduce memory overhead there is no metadata passed into lua – just the pointer.  So whenever an C++ object is accessed in lua, tolua has to look the pointer up in a table or list in order to determine what the object’s type is.  This is a design flaw, because memory pointers are not necessarily unique to a specific class.  Specifically, the first member variable in a C++ class has the same pointer as its parent class.  In the above example, &a and &a.b will have the exact same memory address, even though they are two different objects of different types! So the limitation is that, for two classes interfaced via tolua, one cannot be the very first member variable of the other.

The workaround is straightforward – either rearrange the order of member variables in your class so the interfaced nested class is no longer first, or place a dummy variable in front of it.  If you do not have the flexibility to modify the class you are exporting then you may be in trouble – this would require fixing the design flaw in tolua itself.

I’m posting this in the hopes that it will keep someone else from wasting their time.

My son and daughter both got new DS Lites for Christmas. We purchased Nerf Armor for them both, knowing that they would be heavily used and dropped often. Well, around two months ago, my son dropped his DS, while it was open, onto solid concrete. One quick glance at it was all it took to get that sinking feeling in my stomach – the top LCD panel was toast. A crack ran diagonally from one corner to the other, and only a sliver of screen at the bottom actually rendered pixels. The Nerf Armor didn’t save it, but the fact that the screen was open when it was dropped was probably the main reason it was damaged.

So I began searching for replacement LCD panels. Having done quite a bit of electronic repairs in the past, I decided I would try swapping out the panels myself. I found some at various online retailers for $40-$50, but most were out of stock, and all were priced too high. By the time I would pay shipping and buy the special screwdriver required to open a NDS Lite, I would have had $70 in parts just to attempt a repair on a $129 product. So my son made due with only the bottom screen for a few weeks (hopefully that served as a good lesson in being extra careful with expensive electronic devices). I eventually started looking for LCD panels again, and found one for sale on eBay for $4.98. Yes, you read right. $4.98 USD, with $10 shipping from Hong Kong. I read through the user’s feedback, and it was good, however I learned the shipping would take around two weeks, and that buyers received no communication from the seller at all. So a couple clicks of the mouse, and less than 10 days later the new LCD panel arrived (didn’t receive one single email from the seller as expected).

Read the rest of this entry »