On Friday, 5 March 2021 at 20:28:58 UTC, Ali Çehreli wrote:
On 3/5/21 12:24 PM, Jack wrote:

Are there some kind of replacement or I have to make my own finalize-like method, once I determine somewhat the application no longer need those resources?

destroy() executes the destructor.

but I would need to call it manually and only after I somewhat I've determined I no longer need the resources, right? so destroy(c) would be no different from calling my own finalize-like method like freeResources()?

To my surprise, even though 'c' is not null below, the destructor is not executed multiple times.

import std.stdio;

class C {
  string fileName;

  this(string fileName) {
    writeln("constructing");
    this.fileName = fileName;
    writeln("creating file");
  }

  ~this() {
    writeln("destructing");
    if (fileName) {
      writeln("removing the file");

    } else {
      writeln("NOT removing the file");
    }
  }
}

void main() {
  auto c = new C("some imaginary file name");

  // Executes the destructor
  destroy(c);

  // This does not do anything
  destroy(c);

  // Neither does this
  import core.memory;
  GC.collect();
}

Ali


Reply via email to