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();
Jeroen
  • 15,257
  • 12
  • 59
  • 102
  • Isnt there a this in this language? – Arbitur Apr 07 '14 at 19:32
  • There is, but how to destroy it? As far as I know 'deleting' it won't invoke the destructors. – Jeroen Apr 07 '14 at 19:33
  • possible duplicate of [When to delete in D?](http://stackoverflow.com/questions/4589114/when-to-delete-in-d) – Macmade Apr 07 '14 at 19:35
  • @Macmade No, because I already know delete won't cut it. – Jeroen Apr 07 '14 at 19:36
  • @JeroenBollen Have you read the full answer? The part which speaks about `GC.free( obj )`? – Macmade Apr 07 '14 at 19:36
  • @Macmade That'd work in combination with the destroy function, but there must be a way to destroy an object that is already garbage collected. – Jeroen Apr 07 '14 at 19:40
  • if it's been collected then it has been destroyed – ratchet freak Apr 07 '14 at 19:42
  • @ratchetfreak I meant has been collected as in the GC knows about it. – Jeroen Apr 07 '14 at 19:43
  • if GC knows it can get collected then don't worry about destroying it, if needed implement the destructor but that is it – ratchet freak Apr 07 '14 at 19:45
  • @ratchetfreak My question is how to destroy it though. I want to delete the object (or flag it) and set all pointers to it to null. – Jeroen Apr 07 '14 at 19:47
  • you can't set all pointers to null, the object doesn't know where they all are. There is the global destroy function you can call to reset the object though. the other pointers won't be null but they will see just a fresh object – Adam D. Ruppe Apr 07 '14 at 19:51
  • @AdamD.Ruppe Does that means there isn't a way an object can destroy itself? A fresh object won't do. – Jeroen Apr 07 '14 at 19:52
  • It can call its own destructor and even free its own memory, but it can't set all outside references to null. – Adam D. Ruppe Apr 07 '14 at 19:55
  • @AdamD.Ruppe Is there a way to 'notify' the outside about the object being gone? – Jeroen Apr 07 '14 at 19:56
  • @JeroenBollen not without a custom pointer struct – ratchet freak Apr 07 '14 at 19:59
  • Even a perfect struct couldn't do it because structs can be moved without notice - the parent class couldn't keep track of them. In theory, a precise garbage collector could set pointers to null, scanning all memory for them with type information to avoid false positives, but even if you were changing the D gc that'd be easier said than done. – Adam D. Ruppe Apr 07 '14 at 20:11

3 Answers3

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.

cptroot
  • 321
  • 2
  • 6
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.

Community
  • 1
  • 1
DejanLekic
  • 18,787
  • 4
  • 46
  • 77
0

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

class A {
    public void destroy() {
        state = NULL;
    }
}
Shang
  • 518
  • 3
  • 13
  • That'll just set the pointer to it to null, what if I have other pointers to it? – Jeroen Apr 07 '14 at 19:39
  • then you get dangling pointer which is worse than undeleted state – ratchet freak Apr 07 '14 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 Apr 07 '14 at 19:42
  • @JeroenBollen I meant if you destroy/free something when something points to it then there is a dangling pointer – ratchet freak Apr 07 '14 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 Apr 07 '14 at 19:45
  • Yes it depends on how the function is defined. I assume there wasn't any pointer point to it. if there is you can also do someState -> state = NULL; – Shang Apr 07 '14 at 19:45
  • I have trouble understanding you, what do you mean with State? Have class A be a wrapper around the actual class? – Jeroen Apr 07 '14 at 19:48
  • Please refer to this [link] (http://stackoverflow.com/questions/6767343/how-to-destroy-object-in-itself) – Shang Apr 07 '14 at 19:49
  • @ratchetfreak Simply delete object a from the inside. – Jeroen Apr 07 '14 at 19:50
  • @Shang That's a Java example, and it says the object can't be destroyed. – Jeroen Apr 07 '14 at 19:50
  • @JeroenBollen same thing in any GC language – ratchet freak Apr 07 '14 at 19:52
  • @JeroenBollen that is why you set it to null. – Shang Apr 07 '14 at 19:52
  • @Shang But you set state to null. What is state? Is it 'this'? If so, that won't clear up all pointers. – Jeroen Apr 07 '14 at 19:53
  • @ratchetfreak I don't see how this is related to garbage collection. – Jeroen Apr 07 '14 at 19:54
  • @JeroenBollen GC lets unrelated pointers exist, and doesn't provide any way to null out regular pointers on destruction of an object – ratchet freak Apr 07 '14 at 19:59
  • @ratchetfreak Do not GC'd languages allow you to null it out? :o – Jeroen Apr 07 '14 at 19:59
  • @JeroenBollen the state you set it to null depends on the function. For example if you are trying to clean up the thread you set the object to null. If you are just simply want to destroy as an exit then call sys.exit(0), if it is a process from java.lang.Process.destroy() then you don't return anything. etc – Shang Apr 07 '14 at 20:02
  • @Shang Setting the object to null will work, but that won't clear up the references. – Jeroen Apr 07 '14 at 20:04
  • @JeroenBollen why are you worry about the references? When JVM is on an edge of OutOfMemoryError, it will run the GC. – Shang Apr 07 '14 at 20:11
  • @Shang Because there is no way for the references to know about the object being gone, is there? – Jeroen Apr 07 '14 at 20:11