lua-users home
lua-l archive

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

Fabien wrote:

I have a memory leak problem: some userdata objects are never GC'd, and I really fail to find where it's referenced. Is there a well-known recipe to help tracking down such leaks, if I know which userdata I'm chasing?

one common issue with userdata is that it's hard to make the lua GC account for it's full weight.

the worst (and still very common) case is when you use a third party library to alloc a complex and heavy object, and create a userdata to hold just the pointer. The Lua CG will count only 4 bytes, unaware of the big memory hog. you can find yourself with thousands of these objects, and the CG doesn't bother to delete them.

the usual solution is to add a 'close' method to the userdata, and remember to close any object you don't need anymore.

of course, there's always the "but the CG _should_ do this for me!" argument; but think about open files, or DB connections... it's obvious they should be closed ASAP, CG or not. just the same with any resource-hogging library.