Files
harbour-core/tests/destruct.prg
Viktor Szakats 58faf91453 2016-01-14 19:17 UTC+0100 Viktor Szakats (vszakats users.noreply.github.com)
* *
    % remove brandings and homepage [1] from copyright header. Pass 1 - using script.
      [1] nobody has access to it anymore AFAIK - and it's also just
          a redirect since long
    ! update url in copyright header
    ; this should make the diff between 3.4 and 3.2 easier to manage
2016-01-14 19:18:17 +01:00

127 lines
2.5 KiB
Plaintext

/*
* example/test code for object destructors
*
* Copyright 2006 Przemyslaw Czerpak <druzus / at / priv.onet.pl>
*
*/
#include "hbclass.ch"
MEMVAR P
PROCEDURE Main()
LOCAL bError
PUBLIC P := NIL
bError := ErrorBlock( {| oErr | myErrorHandler( oErr ) } )
? "First simple tests when object is not destroyed by GC"
? "====================================================="
SIMPLETEST( 0 )
SIMPLETEST( 1 )
SIMPLETEST( 2 )
SIMPLETEST( 3 )
?
? "Now object will be destroyed by GC"
? "=================================="
GCFREETEST( 0 )
GCFREETEST( 1 )
GCFREETEST( 2 )
GCFREETEST( 3 )
ErrorBlock( bError )
?
? "*** END OF TEST ***"
RETURN
STATIC PROCEDURE SIMPLETEST( type )
LOCAL o
?
? "=> o := myClass():new( " + hb_ntos( type ) + " )"
o := myClass():new( type )
? "=> o:className() ->", o:className()
? "=> o := NIL"
BEGIN SEQUENCE
o := NIL
END SEQUENCE
RETURN
STATIC PROCEDURE GCFREETEST( type )
LOCAL o, a
?
? "=> o := myClass():new( " + hb_ntos( type ) + " )"
o := myClass():new( type )
? "=> o:className() ->", o:className()
? "=> create corss reference: a := { o, NIL }; a[ 2 ] := a; a := NIL"
a := { o, NIL }; a[ 2 ] := a; a := NIL
? "=> o := NIL"
BEGIN SEQUENCE
o := NIL
END SEQUENCE
? "=> hb_gcAll()"
BEGIN SEQUENCE
hb_gcAll()
END SEQUENCE
RETURN
STATIC FUNCTION myErrorHandler( oErr )
? "Error ->", hb_ntos( oErr:gencode ), ;
oErr:description + ":", oErr:operation
BREAK oErr
RETURN NIL
CREATE CLASS myClass
VAR TYPE
VAR var1
CLASS VAR var2
METHOD INIT
DESTRUCTOR dtor
END CLASS
METHOD INIT( type ) CLASS myClass
? "Hi, I'm INIT method of class:", self:classname()
::type := type
RETURN self
PROCEDURE DTOR CLASS myClass
? " Hi, I'm desturctor of class: ", self:classname()
IF ::type == 1
? " I'm storing reference to self in instance variable."
? " Bad practice but safe in Harbour because it will be destroyed."
::var1 := self
ELSEIF ::Type == 2
? " I'm storing reference to self in class variable."
? " It's programmer bug which should cause RT error."
::var2 := self
ELSEIF ::Type == 3
? " I'm storing reference to self in public variable."
? " It's programmer bug which should cause RT error."
P := self
ELSE
? " I do not store any references to self."
? " It's a safe destructor."
ENDIF
RETURN