Method lfun::_destruct()

Method _destruct

void _destruct(void|int reason)


Object destruction callback.

This function is called right before the object is destructed. That can happen either through a call to destruct(), when there are no more references to the object, or when the garbage collector discovers that it's part of a cyclic data structure that has become garbage.

Parameter reason

A flag that tells why the object is destructed:


Destructed explicitly by destruct.


Destructed due to running out of references.


Destructed by the garbage collector.


Destructed as part of the cleanup when the pike process exits. Occurs only if Pike has been compiled with the configure option --with-cleanup-on-exit. See note below.


Objects are normally not destructed when a process exits, so _destruct functions aren't called then. Use atexit to get called when the process exits.


Regarding destruction order during garbage collection:

If an object is destructed by the garbage collector, it's part of a reference cycle with other things but with no external references. If there are other objects with _destruct functions in the same cycle, it becomes a problem which to call first.

E.g. if this object has a variable with another object which (directly or indirectly) points back to this one, you might find that the other object already has been destructed and the variable thus contains zero.

The garbage collector tries to minimize such problems by defining an order as far as possible:

  • If an object A contains an lfun::_destruct and an object B does not, then A is destructed before B.

  • If A references B single way, then A is destructed before B.

  • If A and B are in a cycle, and there is a reference somewhere from B to A that is weaker than any reference from A to B, then A is destructed before B.

  • If a cycle is resolved according to the rule above by ignoring a weaker reference, and there is another ambiguous cycle that would get resolved by ignoring the same reference, then the latter cycle will be resolved by ignoring that reference.

  • Weak references (e.g. set with set_weak_flag()) are considered weaker than normal references, and both are considered weaker than strong references.

  • Strong references are those from objects to the objects of their lexically surrounding classes. There can never be a cycle consisting only of strong references. (This means the gc never destructs a parent object before all children have been destructed.)

An example with well defined destruct order due to strong references:

class Super {
  class Sub {
    protected void _destruct() {
      if (!Super::this)
        error ("My parent has been destructed!\n");
  Sub sub = Sub();
  protected void _destruct() {
    if (!sub)
      werror ("sub already destructed.\n");

The garbage collector ensures that these objects are destructed in an order so that werror in Super is called and not error in Sub.


When the garbage collector calls lfun::_destruct, all accessible non-objects and objects without _destruct functions are still intact. They are not freed if the _destruct function adds external references to them. However, all objects with lfun::_destruct in the cycle are already scheduled for destruction and will therefore be destroyed even if external references are added to them.

See also

lfun::create(), destruct()