On Wednesday, 7 October 2015 at 07:35:05 UTC, ponce wrote:
On Wednesday, 7 October 2015 at 07:24:03 UTC, Paulo Pinto wrote:

That no, but this yes (at least in C#):

using (LevelManager mgr = new LevelManager())
{
     //....
     // Somewhere in the call stack
     Texture text = mgr.getTexture();
}
--> All level resources gone that require manual management gone
--> Ask the GC to collect the remaining memory right now

If not level wide, than maybe scene/section wide.

However I do get that not all architectures are amendable to be re-written in a GC friendly way.

But the approach is similar to RAII in C++, reduce new to minimum and allocate via factory functions that work together with handle manager classes.

--
Paulo

This is similar to Scoped!T in D. But this is not composable either. You cannot have a "using()" field in a class object, much like you cannot have a Scoped!T field in D. In C#, you still have to implement IDispose interface AFAIK.

You must implement IDisposable.

using(obj = new Obj()) {
  obj.DoSomething();
}

is syntactic sugar transformed by the C# compiler in:

obj = new Obj();
try {
  obj.DoSomething();
}
finally {
  if (obj != null) (IDisposable)obj.Dispose();
}

This can be easily translated to D using a delegate instead of the using block, if someone is keen to use this pattern. More than that, due to TLS, there is no need to worry about thread safety in this case.

Resources are discarded in Dispose(), the class destructor is just calling Dispose().









Reply via email to