www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Cleanup class after method?

reply JN <666total wp.pl> writes:
Imagine I have a very short-lived class:

void print(File f)
{
     PrinterManager pm = new PrinterManager();
     pm.print(f);
}

My understanding is that PrinterManager will be GC allocated, and 
when it goes out of scope, the GC will possibly clean it up at 
some point in the future. But I know that this class won't be 
used anywhere, I want to clean it up right now so that GC doesn't 
waste time later. In C++ it'd be handled by RAII, pm would be a 
unique_ptr<PrinterManager>. How to do it in D?
Jul 04 2018
next sibling parent Mr.Bingo <Bingo Namo.com> writes:
On Wednesday, 4 July 2018 at 15:47:25 UTC, JN wrote:
 Imagine I have a very short-lived class:

 void print(File f)
 {
     PrinterManager pm = new PrinterManager();
     pm.print(f);
 }

 My understanding is that PrinterManager will be GC allocated, 
 and when it goes out of scope, the GC will possibly clean it up 
 at some point in the future. But I know that this class won't 
 be used anywhere, I want to clean it up right now so that GC 
 doesn't waste time later. In C++ it'd be handled by RAII, pm 
 would be a unique_ptr<PrinterManager>. How to do it in D?
https://dlang.org/phobos/std_typecons.html#scoped
Jul 04 2018
prev sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Wednesday, July 04, 2018 15:47:25 JN via Digitalmars-d-learn wrote:
 Imagine I have a very short-lived class:

 void print(File f)
 {
      PrinterManager pm = new PrinterManager();
      pm.print(f);
 }

 My understanding is that PrinterManager will be GC allocated, and
 when it goes out of scope, the GC will possibly clean it up at
 some point in the future. But I know that this class won't be
 used anywhere, I want to clean it up right now so that GC doesn't
 waste time later. In C++ it'd be handled by RAII, pm would be a
 unique_ptr<PrinterManager>. How to do it in D?
The typical thing to do in that case is to use a struct, not a class, since structs have RAII, and they don't need to be allocated on the heap. However, if you're stuck with a class, you can use scope. e.g. scope pm = new PrintManager; In that case, the class will actually be allocated on the stack instead. However, I would point out that until -dip1000 becomes the normal behavior, using scope like this is unsafe, because you have serious problems if any reference to the class object escapes, since it's stack-allocated instead of heap-allocated, and if any reference to it escapes, then it's referring to an invalid object. If you're careful, it's fine, but it is a risk, and regardless, using a struct is preferable if it's possible. -dip1000 fully implements scope so that it verifies that no reference escapes, but it's not ready yet, let alone the default behavior. - Jonathan M Davis
Jul 04 2018
parent reply Flaze07 <christianseiji.cs gmail.com> writes:
On Wednesday, 4 July 2018 at 16:02:25 UTC, Jonathan M Davis wrote:
 -dip1000 fully implements scope so that it verifies that no 
 reference escapes, but it's not ready yet, let alone the 
 default behavior.

 - Jonathan M Davis
I read the proposal about -dip1000 ( not all, some ) and there is also a scoped! template in std.typecons, so...is it better to use the scope keyword, or scoped! template
Jul 04 2018
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Thursday, July 05, 2018 05:47:20 Flaze07 via Digitalmars-d-learn wrote:
 On Wednesday, 4 July 2018 at 16:02:25 UTC, Jonathan M Davis wrote:
 -dip1000 fully implements scope so that it verifies that no
 reference escapes, but it's not ready yet, let alone the
 default behavior.

 - Jonathan M Davis
I read the proposal about -dip1000 ( not all, some ) and there is also a scoped! template in std.typecons, so...is it better to use the scope keyword, or scoped! template
Long term, it will be better to use the scope keyword. Short term (i.e. until -dip1000 is the normal behavior or at least ready to use), it may be better to use scoped, since I think that the wrapper protects you at least somewhat - though the main reason that it was originally introduced is that scope on classes was going to deprecated, because we didn't want something that unsafe to be a keyword, but DIP 1000 is changing that. - Jonathan M Davis
Jul 05 2018