private static void freeObject(ref Obj object_) { #if DEBUG_LOG_GC System.Console.WriteLine("{0} free type {1}", object_._mem_id.ToString(), object_.type.ToString()); #endif switch (object_.type) { case ObjType.OBJ_BOUND_METHOD: object_._free(); object_ = null; break; case ObjType.OBJ_CLASS: { ObjClass klass = (ObjClass)object_; Table.freeTable(ref klass.methods); //FREE<ObjClass>(ref object_); object_._free(); object_ = null; break; } case ObjType.OBJ_CLOSURE: { ObjClosure closure = (ObjClosure)object_; FREE_ARRAY <ObjUpvalue>(typeof(ObjUpvalue), ref closure.upvalues, closure.upvalueCount); object_._free(); object_ = null; break; } case ObjType.OBJ_FUNCTION: { ObjFunction function = (ObjFunction)object_; Chunk.freeChunk(ref function.chunk); // the function's byte code object_._free(); object_ = null; break; } case ObjType.OBJ_INSTANCE: { ObjInstance instance = (ObjInstance)object_; Table.freeTable(ref instance.fields); object_._free(); object_ = null; break; } case ObjType.OBJ_NATIVE: { object_._free(); object_ = null; break; } case ObjType.OBJ_STRING: { ObjString string_ = (ObjString)object_; FREE_ARRAY <char>(typeof(char), ref string_.chars, string_.length + 1); object_._free(); object_ = null; break; } case ObjType.OBJ_UPVALUE: cHeap.values.remove(((ObjUpvalue)object_).location); object_._free(); object_ = null; break; } }