FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to get finalizer called?

 
Post new topic   Reply to topic     Forum Index -> MiniD
View previous topic :: View next topic  
Author Message
moechofe



Joined: 07 Oct 2006
Posts: 25
Location: Paris, France

PostPosted: Wed Apr 21, 2010 1:00 am    Post subject: How to get finalizer called? Reply with quote

I made a simple example, to observe that my finalizer function isn't called. Do I forgot something?
Code:
module minidtestdelete;
import tango.util.log.Trace;
import minid.api;
pragma(lib,"DD-minid");

class S
{
   this() { Trace("this"); }
   ~this() { Trace("~this"); }

   static size_t allocator( MDThread* t, size_t num_params )
   {
      Trace("allocator()");
      newInstance( t, 0, 0, S.sizeof );
      dup(t);
      pushNull(t);
      rotateAll(t, 3);
      methodCall(t, 2, "constructor", 0);
      return 1;
   }

   static size_t constructor( MDThread* t, size_t num_params )
   {
      Trace("constructor()");
      auto s = checkInstParam!(S)(t, 0, "S");
      s = cast(S*)new S;
      return 0;
   }

   static size_t finalizer( MDThread* t, size_t num_params )
   {
      Trace("finalizer()");
      auto s = checkInstParam!(S)(t, 0, "S");
      delete s;
      return 0;
   }
}

static ~this() { Trace("static destructor"); }

void main()
{
   Trace(">"); scope(exit) Trace("<");
   MDThread* t;
   MDVM virtual_machine;
   t = openVM( &virtual_machine );
   loadStdlibs( t );
   CreateClass( t, "S", (CreateClass* c)
   {
      c.method("constructor", &S.constructor);
   });
   newFunction(t, &S.allocator, "S.allocator");
   setAllocator(t, -2);
   newFunction(t, &S.finalizer, "S.finalizer");
   setFinalizer(t, -2);
   newGlobal(t, "S");
   runString(t, "local test = S()");
}
And the result is:
Code:
>
allocator()
constructor()
this
<
static destructor
~this


Has you can see, the class destructor is called after the module destructor. Is a problem when using externals resources.

How I can fix that?
Back to top
View user's profile Send private message
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Wed Apr 21, 2010 10:27 pm    Post subject: Reply with quote

Put a "closeVM(&virtual_machine);" at the end of main().
Back to top
View user's profile Send private message
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Wed Apr 21, 2010 10:39 pm    Post subject: Reply with quote

Also I don't really know what the heck you're doing in the constructor.. I think what you want is to use a nativeobj and keep it in the extra fields of the instance. Please, please read the documentation before asking..
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> MiniD All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group