www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - question about delete.

reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
When happens when delete this is done inside a class?
Why doesn't it immediatelly call the destructor?

I wrote a class that is ment to be a temporary object
return by functions and i would like it to be destructed
right after it is used. So i tried:

class ref(Type)
{
 this(inout Type t)
 {
  stdout.writeLine("constructor " ~ string.toString(t));
  elem = &t;
 }
 ~this()
 {
  stdout.writeLine("destructor " ~ string.toString(*elem));
 }
 Type* elem;
 Type val(){return *elem; delete this; }
 Type val(Type t){(*elem)=t; return *elem; delete this;}
}

Inside val i have "delete this" but in this code:

<CODE>
class A
{
     void print()
     {
          stdout.writeLine("A[ x=" ~ std.string.toString(x)~"]");
     }
     ref!(int) returnbyrefx()
     {
          return new ref!(int)(x);
     }
    int x = 10;
}

int main ( char [] [] args )
{
     A a = new A();
     a.print();
     a.returnbyrefx.val = 7;
     a.print();
     getch();
     return 1;
}
</CODE>

i see that the destructor of ref is called when the program is exited.
So what does then "delete this" mean, does it only tell GC that
the object can be collected?
May 03 2004
next sibling parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
Sorry first sentence should have been:
What happens when delete this is done inside a class?

"Ivan Senji" <ivan.senji public.srce.hr> wrote in message
news:c756ct$1jsi$1 digitaldaemon.com...
 When happens when delete this is done inside a class?
 Why doesn't it immediatelly call the destructor?

 I wrote a class that is ment to be a temporary object
 return by functions and i would like it to be destructed
 right after it is used. So i tried:

 class ref(Type)
 {
  this(inout Type t)
  {
   stdout.writeLine("constructor " ~ string.toString(t));
   elem = &t;
  }
  ~this()
  {
   stdout.writeLine("destructor " ~ string.toString(*elem));
  }
  Type* elem;
  Type val(){return *elem; delete this; }
  Type val(Type t){(*elem)=t; return *elem; delete this;}
 }

 Inside val i have "delete this" but in this code:

 <CODE>
 class A
 {
      void print()
      {
           stdout.writeLine("A[ x=" ~ std.string.toString(x)~"]");
      }
      ref!(int) returnbyrefx()
      {
           return new ref!(int)(x);
      }
     int x = 10;
 }

 int main ( char [] [] args )
 {
      A a = new A();
      a.print();
      a.returnbyrefx.val = 7;
      a.print();
      getch();
      return 1;
 }
 </CODE>

 i see that the destructor of ref is called when the program is exited.
 So what does then "delete this" mean, does it only tell GC that
 the object can be collected?
May 03 2004
parent reply vathixSpamFix dprogramming.com (Vathix) writes:
In article <c756j2$1k3h$1 digitaldaemon.com>, ivan.senji public.srce.hr 
says...
Sorry first sentence should have been:
What happens when delete this is done inside a class?

"Ivan Senji" <ivan.senji public.srce.hr> wrote in message
news:c756ct$1jsi$1 digitaldaemon.com...
 When happens when delete this is done inside a class?
 Why doesn't it immediatelly call the destructor?

 I wrote a class that is ment to be a temporary object
 return by functions and i would like it to be destructed
 right after it is used. So i tried:

 class ref(Type)
 {
  this(inout Type t)
  {
   stdout.writeLine("constructor " ~ string.toString(t));
   elem = &t;
  }
  ~this()
  {
   stdout.writeLine("destructor " ~ string.toString(*elem));
  }
  Type* elem;
  Type val(){return *elem; delete this; }
  Type val(Type t){(*elem)=t; return *elem; delete this;}
 }

 Inside val i have "delete this" but in this code:

 <CODE>
 class A
 {
      void print()
      {
           stdout.writeLine("A[ x=" ~ std.string.toString(x)~"]");
      }
      ref!(int) returnbyrefx()
      {
           return new ref!(int)(x);
      }
     int x = 10;
 }

 int main ( char [] [] args )
 {
      A a = new A();
      a.print();
      a.returnbyrefx.val = 7;
      a.print();
      getch();
      return 1;
 }
 </CODE>

 i see that the destructor of ref is called when the program is exited.
 So what does then "delete this" mean, does it only tell GC that
 the object can be collected?
The return statement causes execution to immediately jump back to the caller; the delete statements are not reached in your code. You need to use a temporary variable to hold the return value, then delete the object, and finally return the temp. -- Christopher E. Miller
May 03 2004
parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Vathix" <vathixSpamFix dprogramming.com> wrote in message
news:c75a6q$1nsp$1 digitaldaemon.com...
 In article <c756j2$1k3h$1 digitaldaemon.com>, ivan.senji public.srce.hr
 says...
Sorry first sentence should have been:
What happens when delete this is done inside a class?

"Ivan Senji" <ivan.senji public.srce.hr> wrote in message
news:c756ct$1jsi$1 digitaldaemon.com...
 When happens when delete this is done inside a class?
 Why doesn't it immediatelly call the destructor?

 I wrote a class that is ment to be a temporary object
 return by functions and i would like it to be destructed
 right after it is used. So i tried:

 class ref(Type)
 {
  this(inout Type t)
  {
   stdout.writeLine("constructor " ~ string.toString(t));
   elem = &t;
  }
  ~this()
  {
   stdout.writeLine("destructor " ~ string.toString(*elem));
  }
  Type* elem;
  Type val(){return *elem; delete this; }
  Type val(Type t){(*elem)=t; return *elem; delete this;}
 }

 Inside val i have "delete this" but in this code:

 <CODE>
 class A
 {
      void print()
      {
           stdout.writeLine("A[ x=" ~ std.string.toString(x)~"]");
      }
      ref!(int) returnbyrefx()
      {
           return new ref!(int)(x);
      }
     int x = 10;
 }

 int main ( char [] [] args )
 {
      A a = new A();
      a.print();
      a.returnbyrefx.val = 7;
      a.print();
      getch();
      return 1;
 }
 </CODE>

 i see that the destructor of ref is called when the program is exited.
 So what does then "delete this" mean, does it only tell GC that
 the object can be collected?
The return statement causes execution to immediately jump back to the caller; the delete statements are not reached in your code. You need to
use
 a temporary variable to hold the return value, then delete the object, and
 finally return the temp.
I am such an idiot! I can't believe i didn't see this! Thanks!
 --
 Christopher E. Miller
May 03 2004
prev sibling parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
"Ivan Senji" <ivan.senji public.srce.hr> wrote in message
news:c756ct$1jsi$1 digitaldaemon.com
| When happens when delete this is done inside a class?
| Why doesn't it immediatelly call the destructor?
|
| I wrote a class that is ment to be a temporary object
| return by functions and i would like it to be destructed
| right after it is used. So i tried:
|
| [...]
|
| i see that the destructor of ref is called when the program is exited.
| So what does then "delete this" mean, does it only tell GC that
| the object can be collected?

You may want to use auto objects or auto classes.

-----------------------
Carlos Santander Bernal
May 03 2004
parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Carlos Santander B." <carlos8294 msn.com> wrote in message
news:c766kg$3ad$2 digitaldaemon.com...
 "Ivan Senji" <ivan.senji public.srce.hr> wrote in message
 news:c756ct$1jsi$1 digitaldaemon.com
 | When happens when delete this is done inside a class?
 | Why doesn't it immediatelly call the destructor?
 |
 | I wrote a class that is ment to be a temporary object
 | return by functions and i would like it to be destructed
 | right after it is used. So i tried:
 |
 | [...]
 |
 | i see that the destructor of ref is called when the program is exited.
 | So what does then "delete this" mean, does it only tell GC that
 | the object can be collected?

 You may want to use auto objects or auto classes.
The problem was something else. I can't use auto objects because it isn't possible to return auto created objects.
 -----------------------
 Carlos Santander Bernal
May 03 2004