Re: Another collectgarbage question. (Inherited class)

Taesoo Kwon <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
I changed the code using only LUA functionalities as followes, and garbage
collection works perfectly. So I tends to believe that this's a LUABIND's
problem.
(luabind classes are collected eventually in the lua_close() function.
However, collectgarbage function never worked for a derived luabind class.
I suspect that Luabind might set some cyclic reference somewhere.)

When not using luabind, collectgarbage function always collected an object
whose reference count==0, and one can always make reference count==0 by
manually setting all references to the variable nil. (It can be tedious
sometimes though.) As an example, I attached a sample code including a class
inheritance implementation written by myself below.

This was not the case when the luabind 'class' was used. (Especially, a
derived one.)
Memory was not freed when collectgarbage(..) was called, but freed later in
lua_close() function.
That was unacceptable in my case because a lot of temporary class instances
are generated in a loop, and all though instances were not freed in the loop
even though I called collectgarbage() many times.

---------------------------------------------------------------

function LUAclass(baseClass)

-- A function to define a class only using native lua calls.
-- usage: MotionLoader=LUAclass()  -- define a class.
--        VRMLloader=LUAclass(MotionLoader) -- define a derived class

--  loader=VRMLloader:create({a,b,c}) -- Create an instance of the class.
-- Note that parameters are enclosed by {}

   local classobj={}
   if __classMTs==nil then
      __classMTs={}
      __classMTs.N=0
   end

   __classMTs.N=__classMTs.N+1
   local classId=__classMTs.N
   __classMTs[classId]={__index=classobj}
   classobj.__classId=classId

   classobj.create=function (classobj, tbl)
              local new_inst={}
              setmetatable(new_inst, __classMTs[classobj.__classId])
              new_inst:__init(unpack(tbl))
              return new_inst
           end

   if baseClass~=nil then
      setmetatable(classobj, {__index=baseClass})
   end

   return classobj
end


class 'VVV' -- I used luabind class only here to check if dtor is called.
You can also use a C++ class instead.
function VVV:__init(ss)
   print("ctor VVV",ss)
   self.name=ss
end

function VVV:__finalize()
   print("dtor VVV", self.name)
end

GCtest=LUAclass()

function GCtest:__init(ss)

   self.skel=VVV(ss)

end



GCtest2=LUAclass(GCtest)
function GCtest2:__init(ss)
   GCtest.__init(self,ss)
end

function gcTest()
   do
      local test=GCtest:create({"name1"})
   end
   collectgarbage("collect") -- dtor of VVV called

   do
      local test=GCtest2:create({"name2"})
   end
   collectgarbage("collect") -- dtor of VVV called

end




On Fri, Sep 25, 2009 at 4:48 AM, Jason McKesson <[email protected]> wrote:

> Taesoo Kwon wrote:
> > Hello,
> >
> > I am using luabind 0.8.1, lua5.1.4, MSVC2005.
> > I have an example where garbage collection doesn't work for a derived
> > lua class.
> > Is this a normal behavior? What should I modify?
> >
> > class 'VVV'
> >
> > function VVV:__init()
> >    print("ctor VVV")
> > end
> >
> > function VVV:__finalize()
> >    print("dtor VVV")
> > end
> >
> >
> > class 'GCtest'
> >
> > function GCtest:__init()
> >    self.skel=VVV()
> > end
> >
> > class 'GCtest2'(GCtest) -- derived class
> > function GCtest2:__init()
> >    GCtest.__init(self)
> > end
> >
> > function gctest()
> >    do
> >       local test=GCtest()
> >    end
> >    collectgarbage() -- dtor of VVV is called.
> >    collectgarbage()
> >    collectgarbage()
> >
> >    do
> >       local test=GCtest2()
> >    end
> >    collectgarbage()
> >    collectgarbage()
> >    collectgarbage() -- dtor of VVV is not called.
> >
> > end
> >
> > Thank you.
> > ------------------------------------------------------------------------
> >
> >
> ------------------------------------------------------------------------------
> > Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
> > is the only developer event you need to attend this year. Jumpstart your
> > developing skills, take BlackBerry mobile applications to market and stay
> > ahead of the curve. Join us from November 9&#45;12, 2009. Register
> now&#33;
> > http://p.sf.net/sfu/devconf
> > ------------------------------------------------------------------------
> >
> > _______________________________________________
> > luabind-user mailing list
> > [email protected]
> > https://lists.sourceforge.net/lists/listinfo/luabind-user
> >
> It is Lua that decides when the finalize method of a table will be
> called. It will do it eventually, but it does not guarantee that it will
> be called at any particular time. And if Lua's garbage collection
> functions don't want to do a full CG on everything in memory when you
> call the function, then there's nothing you can do.
>
> This is the price you pay for having a fast garbage collector.
>
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
> http://p.sf.net/sfu/devconf
> _______________________________________________
> luabind-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf

_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.