Here is a small example.
Code: Select all
Declare FreeStrX(*This)
Declare FreeStrY(*This)
Structure y
*Point
File.i
Image.i
Destructor FreeStrY()
EndStructure
Structure x
*Struct
Mutex.i
List l.y()
Destructor FreeStrX()
EndStructure
Procedure FreeStrX(*This.X)
If *This\Struct
FreeStructure(*This\Struct)
*This\Struct=0
EndIf
If *This\Mutex
FreeMutex(*This\Mutex)
*This\Mutex=0
EndIf
EndProcedure
Procedure FreeStrY(*This.Y)
If *This\Point
FreeMemory(*This\Point)
*This\Point=0
EndIf
If *This\File And IsFile(*This\File)
CloseFile(*This\File)
*This\File=0
EndIf
If *This\Image And IsImage(*This\Image)
FreeImage(*This\Image)
*This\Image=0
EndIf
EndProcedure
Procedure Test()
Protected s.x
s\Struct = AllocateStructure(y)
s\Mutex = CreateMutex()
For i=1 To 10
If AddElement(s\l())
s\l()\Point = AllocateMemory(100)
s\l()\File = OpenFile(#PB_Any, "File"+i)
s\l()\Image = CreateImage(#PB_Any, 10, 10)
EndIf
Next
EndProcedure
Test()
End
The procedure FreeStrY() will be called 11 times (1 time from the procedure FreeStrX() when calling the function FreeStructure() and called 10 when implicitly freeing structures from the list s\l()).
Procedure FreeStrX() will be called once, with the implicit release of the structure X when the Test() procedure is completed.
It may also make sense to add a constructor to the structures.