1

If I have a D object, how can I destroy that D object from the inside? (Have it destroy itself)

class A {
    public void destoy() {
        // Destroy me! 
    }
}

A a = new A;
a.destroy();
16
  • Isnt there a this in this language?
    – Arbitur
    Commented Apr 7, 2014 at 19:32
  • There is, but how to destroy it? As far as I know 'deleting' it won't invoke the destructors.
    – Jeroen
    Commented Apr 7, 2014 at 19:33
  • possible duplicate of When to delete in D?
    – Macmade
    Commented Apr 7, 2014 at 19:35
  • @Macmade No, because I already know delete won't cut it.
    – Jeroen
    Commented Apr 7, 2014 at 19:36
  • @JeroenBollen Have you read the full answer? The part which speaks about GC.free( obj )?
    – Macmade
    Commented Apr 7, 2014 at 19:36

3 Answers 3

1

No, there is no way to do this in D, and there are good reasons for it. This SO thread explains how it can be done in C++, and explains the threats of delete this ...

IMHO, it is a good idea D disallows this, because it seems logical - objects should not control their own lifecycle, program should do that. Program "gave life" to objects, program should "take their lives" (garbage-collect them in this case) too.

1

Unfortunately, there is no way for an object to know about any external references to it.

To get around this, you can add a method that keeps track of fields storing pointers to the object. However, this is clunky, and results in trying to modify external data.

You could add a set of signals and slots to your object, enabling it to tell all who will listen that it wishes to be destroyed. However, this forces external code to add extra methods for each reference to your object they wish to use.

Hopefully there is a way to modify your algorithm such that it does not depend on getting garbage collected at an explicit time, or on generating dangling pointers, one of the problems that D is meant to eradicate.

0

Make it whatever you want to destroy equal to 'NULL' for example

class A {
    public void destroy() {
        state = NULL;
    }
}
20
  • That'll just set the pointer to it to null, what if I have other pointers to it?
    – Jeroen
    Commented Apr 7, 2014 at 19:39
  • then you get dangling pointer which is worse than undeleted state Commented Apr 7, 2014 at 19:41
  • @ratchetfreak Surely that'll end up messing something up. It'll be pointing to a non-object or simply to the not-yet-collected object.
    – Jeroen
    Commented Apr 7, 2014 at 19:42
  • @JeroenBollen I meant if you destroy/free something when something points to it then there is a dangling pointer Commented Apr 7, 2014 at 19:44
  • @ratchetfreak I know. If I set this to null, that's only one pointer set to null, isn't it? Am I not understanding something here? What if I have multiple references to the same object?
    – Jeroen
    Commented Apr 7, 2014 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.