xHarbour Reference Documentation > Statement Reference xHarbour Developers Network  

DESTRUCTOR

Declares a method to be called by the garbage collector.

Syntax

DESTRUCTOR <MethodName>

Arguments

<MethodName>
This is the symbolic name of the destructor method. It must begin with a letter or underscore followed by digits, letters or underscores. The symbolic name can contain up to 63 characters.

Description

The DESTRUCTOR statement can only be used within the class declaration between CLASS and ENDCLASS. It declares a method that is automatically called when an object becomes subject to garbage collection. This is the case when there is no memory variable left holding a reference to an object.

The implementation of the destructor method must follow the ENDCLASS statement and must use the PROCEDURE statement, rather than METHOD. This is required since a destructor method cannot have a return value. The implementation of a destructor method follows this coding pattern:

   PROCEDURE <MethodName> CLASS <ClassName>
      <cleanup code>
   RETURN

Destructor methods provide a convenient way of explicitely "cleaning up" memory or calling maintenance routines, like closing open files managed by an object.

Info

See also:CLASS, ERROR HANDLER, PROCEDURE
Category: Class declaration , Declaration , xHarbour extensions
Header:hbclass.ch
Source:vm\classes.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example outlines usage of a destructor method
// being called when an object becomes subject to
// garbage collection. The output of the example shows
// the sequence of destruction as the objects become
// inaccessible in the course of the program.

   #include "HbClass.ch"

   PROCEDURE Main
      LOCAL obj := Test():new( "First object" )

      TestProc()
   RETURN

   // object stored in LOCAL variable
   PROCEDURE TestProc()
      LOCAL obj    := Test():new( "Second object" )
      LOCAL bBlock := TestFunc()
   RETURN

   // object stored in PUBLIC variable is released
   // object stored in detached LOCAL is returned
   FUNCTION TestFunc()
      LOCAL  bBlock := TestBlock()
      object := NIL
   RETURN bBlock

   // objects stored in detached LOCAL variable and PUBLIC variable
   FUNCTION TestBlock()
      LOCAL obj := Test():new( "Third object" )

      PUBLIC object := Test():new( "Fourth object" )
   RETURN {|| obj:name }


   CLASS Test
      EXPORTED:
      DATA   name
      METHOD init

      DESTRUCTOR destroy                  // declaration of destructor
   ENDCLASS

   METHOD init( cName ) CLASS Test
      ::name := cName
   RETURN self

   PROCEDURE destroy CLASS Test           // implementation of destructor
     ? "Object:", ::name, "Destroyed in:", ProcName(1)
   RETURN

   // **************** Output ********************
   // Object: Fourth object Destroyed in: TESTFUNC
   // Object: Third object Destroyed in: TESTPROC
   // Object: Second object Destroyed in: TESTPROC
   // Object: First object Destroyed in: MAIN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe