C interface, destructor and __gc question

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

C interface, destructor and __gc question

Laurent FAILLIE
Hello,

I would like to have some clarification about the way I can implement à destructor of a C object.
As per http://www.lua.org/pil/29.1.html, it should be implemented using __gc but ... when will it be called.

I have an object that is managing a windows and I would like to ensure this windows is destroyed as soon as it's representing object is not anymore used.

In other words,
for a code like

if somestuff then
    wnd = SelSurface.create()
    ... bla bla bla ..
end

I would like to be sure the window is destroyed as soon as the code leave the "if" statement and not when the GC decided to run.

Thanks

Laurent
 
----
The misspelling master is on the Web.
  _________ 100 % Dictionnary Free !
/               /(
/ Dico       / / Pleins d'autres fautes sur
/________/ /
(#######( / http://destroyedlolo.info
Quoi, des fautes d'orthographe! Pas possible ;-D.
Reply | Threaded
Open this post in threaded view
|

RE: C interface, destructor and __gc question

Thijs Schreijer

Op 15 apr. 2015 10:02 schreef Laurent FAILLIE <[hidden email]>:

>
> Hello,
>
> I would like to have some clarification about the way I can implement à destructor of a C object.
> As per http://www.lua.org/pil/29.1.html, it should be implemented using __gc but ... when will it be called.
>
> I have an object that is managing a windows and I would like to ensure this windows is destroyed as soon as it's representing object is not anymore used.
>
> In other words,
> for a code like
>
> if somestuff then
>     wnd = SelSurface.create()
>     ... bla bla bla ..

I think the only way is to manually destroy it. Insert something like;
     wnd:destroy()
Or manually run a GC cycle.
If you create a userdata, have a flag for it being alive or closed. When calling destroy, get rid of everything you need to clean up and flag it as closed. Any further access of the closed userdata should now result in an error. Still leaves the user data itself to be cleaned up by the GC.

> end
>
> I would like to be sure the window is destroyed as soon as the code leave the "if" statement and not when the GC decided to run.
>
> Thanks
>
> Laurent
>  
> ----
> The misspelling master is on the Web.
>   _________ 100 % Dictionnary Free !
> /               /(
> / Dico       / / Pleins d'autres fautes sur
> /________/ /
> (#######( / http://destroyedlolo.info
> Quoi, des fautes d'orthographe! Pas possible ;-D.

Reply | Threaded
Open this post in threaded view
|

Re: C interface, destructor and __gc question

Coda Highland
On Wed, Apr 15, 2015 at 8:03 AM, Thijs Schreijer
<[hidden email]> wrote:

>
> Op 15 apr. 2015 10:02 schreef Laurent FAILLIE <[hidden email]>:
>>
>> Hello,
>>
>> I would like to have some clarification about the way I can implement à destructor of a C object.
>> As per http://www.lua.org/pil/29.1.html, it should be implemented using __gc but ... when will it be called.
>>
>> I have an object that is managing a windows and I would like to ensure this windows is destroyed as soon as it's representing object is not anymore used.
>>
>> In other words,
>> for a code like
>>
>> if somestuff then
>>     wnd = SelSurface.create()
>>     ... bla bla bla ..
>
> I think the only way is to manually destroy it. Insert something like;
>      wnd:destroy()
> Or manually run a GC cycle.
> If you create a userdata, have a flag for it being alive or closed. When calling destroy, get rid of everything you need to clean up and flag it as closed. Any further access of the closed userdata should now result in an error. Still leaves the user data itself to be cleaned up by the GC.
>

To follow up on this: Lua, like Java, doesn't have proper destructors
like C++ does. __gc is more of a finalizer than a destructor -- a way
to clean up resources if they're found to not be in use, rather than
an explicit shutdown routine.

Manually running a GC cycle isn't necessarily going to do the job
anyway -- for one thing, in some cases you have to run two sweeps to
eject objects; for another, if something somewhere else is holding a
reference then the GC won't do anything either. An explicit destroy is
necessary if you need to ensure certain properties with a certain
deadline.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: C interface, destructor and __gc question

Laurent FAILLIE
I found an alternate way, using pcall() like for try/catch mechanism http://www.lua.org/pil/8.4.html but it's not a clean way for me as the "user" will have to cheat on client lua code :(
destructor looks to me a feature that has to be thought for future release.
 
The misspelling master is on the Web.
  _________ 100 % Dictionnary Free !
/               /(
/ Dico       / / Pleins d'autres fautes sur
/________/ /
(#######( / http://destroyedlolo.info
Quoi, des fautes d'orthographe! Pas possible ;-D.



Le Mercredi 15 avril 2015 18h16, Coda Highland <[hidden email]> a écrit :


On Wed, Apr 15, 2015 at 8:03 AM, Thijs Schreijer
<[hidden email]> wrote:

>
> Op 15 apr. 2015 10:02 schreef Laurent FAILLIE <[hidden email]>:
>>
>> Hello,
>>
>> I would like to have some clarification about the way I can implement à destructor of a C object.
>> As per http://www.lua.org/pil/29.1.html, it should be implemented using __gc but ... when will it be called.
>>
>> I have an object that is managing a windows and I would like to ensure this windows is destroyed as soon as it's representing object is not anymore used.
>>
>> In other words,
>> for a code like
>>
>> if somestuff then
>>    wnd = SelSurface.create()
>>    ... bla bla bla ..
>
> I think the only way is to manually destroy it. Insert something like;
>      wnd:destroy()
> Or manually run a GC cycle.
> If you create a userdata, have a flag for it being alive or closed. When calling destroy, get rid of everything you need to clean up and flag it as closed. Any further access of the closed userdata should now result in an error. Still leaves the user data itself to be cleaned up by the GC.
>

To follow up on this: Lua, like Java, doesn't have proper destructors
like C++ does. __gc is more of a finalizer than a destructor -- a way
to clean up resources if they're found to not be in use, rather than
an explicit shutdown routine.

Manually running a GC cycle isn't necessarily going to do the job
anyway -- for one thing, in some cases you have to run two sweeps to
eject objects; for another, if something somewhere else is holding a
reference then the GC won't do anything either. An explicit destroy is
necessary if you need to ensure certain properties with a certain
deadline.

/s/ Adam