Files
harbour-core/harbour/tests/destruct.prg
Viktor Szakats 986df3694e 2012-09-30 23:08 UTC+0200 Viktor Szakats (harbour syenar.net)
+ contrib/hbct/tests/dates4.prg
  - tests/dates4.prg
  * contrib/hbnf/byt2bit.prg
  * contrib/hbnf/dectobin.prg
  * contrib/hbnf/popadder.prg
  * tests/ac_test2.prg
  * tests/ainstest.prg
  * tests/and_or.prg
  * tests/array16.prg
  * tests/arrays.prg
  * tests/begin.prg
  * tests/byref.prg
  * tests/calling.prg
  * tests/clasinh.prg
  * tests/clasinit.prg
  * tests/classes.prg
  * tests/clsnv.prg
  * tests/codebloc.prg
  * tests/dates.prg
  * tests/debugtst.prg
  * tests/destruct.prg
  * tests/dirtest.prg
  * tests/dynobj.prg
  * tests/exittest.prg
  * tests/fib.prg
  * tests/files.prg
  * tests/fornext.prg
  * tests/fsplit.prg
  * tests/gtchars.prg
  * tests/ifelse.prg
  * tests/inherit.prg
  * tests/inifiles.prg
  * tests/initexit.prg
  * tests/inkeytst.prg
  * tests/inline.prg
  * tests/iotest.prg
  * tests/iotest2.prg
  * tests/longdev.prg
  * tests/longstr2.prg
  * tests/memvar.prg
  * tests/multiarg.prg
  * tests/newrdd.prg
  * tests/nums.prg
  * tests/objasign.prg
  * tests/objects.prg
  * tests/overload.prg
  * tests/passref.prg
  * tests/procname.prg
  * tests/readhrb.prg
  * tests/returns.prg
  * tests/rto_get.prg
  * tests/rto_tb.prg
  * tests/sbartest.prg
  * tests/setkeys.prg
  * tests/speed.prg
  * tests/statfun.prg
  * tests/statics.prg
  * tests/stripem.prg
  * tests/switch.prg
  * tests/tb1.prg
  * tests/test_all.prg
  * tests/testbrw.prg
  * tests/testcls.prg
  * tests/testerro.prg
  * tests/testfor.prg
  * tests/testmem.prg
  * tests/testntx.prg
  * tests/testop.prg
  * tests/testpp.prg
  * tests/testrdd2.prg
  * tests/teststr.prg
  * tests/testvars.prg
  * tests/testwarn.prg
  * tests/tstasort.prg
  * tests/tstdbi.prg
  * tests/tstmacro.prg
  * tests/varparam.prg
  * tests/vidtest.prg
    * various cleanups, fixes and formatting
      now most tests are warning and error free
2012-09-30 21:12:01 +00:00

133 lines
2.6 KiB
Plaintext

/*
* $Id$
*/
/*
* Harbour Project source code:
* example/test code for object destructors
*
* Copyright 2006 Przemyslaw Czerpak <druzus / at / priv.onet.pl>
* www - http://harbour-project.org
*
*/
#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
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
? "=> hb_gcAll()"
BEGIN SEQUENCE
hb_gcAll()
END
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