Another collectgarbage question. (Inherited class)

View: New views
4 Messages — Rating Filter:   Alert me  

Another collectgarbage question. (Inherited class)

by Taesoo Kwon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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® 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-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
luabind-user mailing list
luabind-user@...
https://lists.sourceforge.net/lists/listinfo/luabind-user

Re: Another collectgarbage question. (Inherited class)

by Jason McKesson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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® 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-12, 2009. Register now!
> http://p.sf.net/sfu/devconf
> ------------------------------------------------------------------------
>
> _______________________________________________
> luabind-user mailing list
> luabind-user@...
> 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® 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-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
luabind-user mailing list
luabind-user@...
https://lists.sourceforge.net/lists/listinfo/luabind-user

Re: Another collectgarbage question. (Inherited class)

by Taesoo Kwon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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 <korval2@...> 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
> luabind-user@...
> 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
luabind-user@...
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
Come build with us! The BlackBerry® 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-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
luabind-user mailing list
luabind-user@...
https://lists.sourceforge.net/lists/listinfo/luabind-user

[Solved] Another collectgarbage question. (Inherited class)

by Taesoo Kwon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Nigel Atkinson sent me an e-mail regarding this issue. It worked, so I am attaching it here for those who experience a similar problem. Thanks!
-------------------------------------------------------------------------------------------------
I hit the same situation.  As it turns out when you make an instance of a
Lua class, that has a base class, it makes a global called 'super'.  This
was for the old method of calling the base constructor.  I think you will
find it is only the *last* instance (that fits the above description)
created that is affected.

So doing:

super = nil

before collecting garbage, it should work.  I personally commented out the
C++ code that makes the 'super' global, as its use is now depreciated.

Hope this helps!

--

Nigel Atkinson
nigel@...


On Wed, Sep 23, 2009 at 5:38 PM, Taesoo Kwon <taesoobear@...> 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()  -- <<< <  add "super=nil" after here. Then dtor of VVV is called.
   end
   collectgarbage()
   collectgarbage()
   collectgarbage() -- dtor of VVV is not called.

end

Thank you.


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
luabind-user mailing list
luabind-user@...
https://lists.sourceforge.net/lists/listinfo/luabind-user