www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Writing a library

reply Mn <mn mailinator.com> writes:
Jarrett Billingsley Wrote:

One way of doing this for classes is to keep a 
 static list or AA of all instances of the class in the class:
 
 class ExportedClass
 {
     static bool[ExportedClass] instances;
 
     this()
     {
         ....
         instances[this] = true;
     }
 
     ~this()
     {
         instances.remove(this);
     }
 }
 

But now if I somehow call a create()-function (or however i call it) it creates a class instance and returns it. The class adds a reference to that instance to its instances list. But then we return from that create() function to the program outside of the lib/dll - and the instances list? Doesnt it get removed by GC? How long does it (the instances list) stay in memory (or wherever it is)? From first call to OS shutdown? Or to host app shutdown? Or to ________ ? --Mn
Feb 17 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Mn wrote:
 Jarrett Billingsley Wrote:
 
 One way of doing this for classes is to keep a 
 static list or AA of all instances of the class in the class:

 class ExportedClass
 {
     static bool[ExportedClass] instances;

     this()
     {
         ....
         instances[this] = true;
     }

     ~this()
     {
         instances.remove(this);
     }
 }

But now if I somehow call a create()-function (or however i call it) it creates a class instance and returns it. The class adds a reference to that instance to its instances list. But then we return from that create() function to the program outside of the lib/dll - and the instances list? Doesnt it get removed by GC? How long does it (the instances list) stay in memory (or wherever it is)? From first call to OS shutdown? Or to host app shutdown? Or to ________ ?

It's a static variable in a DLL, so it stays in memory from the time the DLL is loaded to when it's unloaded (which may be at application shutdown if it's never explicitly unloaded).
Feb 17 2007