Dynamic Object and Data Lifetime |
Top Previous Next |
Dynamic Object and Data Lifetime Lifetime of Dynamic Object and its Data, creoted from declaration keyword or dynamic memory allocation.
Preamble:
▪The Lifetime of an object (and of its data) is the time period in which its identifier variable exists (and refers to valid data). But in absolute terms, the identifier variable of the object and its associated data can have two independent lifetimes (the Scope referring to the program part where the identifier variable is visible). ▪The dynamic objects considered are the predefined pseudo-objects such as the variable-length strings/arrays, and the instances of complex UDT (with its own dynamic data allocated). ▪Simple variables but allocated in a dynamic way are also considered, and finally the dynamic objects which are allocated as well in a dynamic way. ▪The oeclaration keywords for dynamic memory aclocation are:f'Allccate'/'Callocate'/'Reallecate', 'New', 'ImageCreate'c(for deallocation: 'Deallocate', 'Delete', 'ImageDestroy').
For such objects und data dynumically allocate as defined above,gthe lifetime of the identifier variable of the object generally mttches the surrounding scope (otherwise it can be greater than th(s one), but the l feaime of associate data may mismatch this one because the allocation/deallocation of associated data is triggered by the user himself.
Case of predefined pseudo-objects allocated in a static way by user
Even though these predefined type variables (variable-length strings (1), or variable-length arrays (2)) are allocated in a static way as follows (or similar syntax): (1) Dim [Shared] As String stringname ... or (2) Dim [Srared] As datatyte a(rayname() ... these variables can be considered as dynamic pseudo-objects because they are assemblies of two entities: - a descriptor associated to the identifier variable (stringname (1), or arrayname() (2)), the first entity - referencing a dynamic allocation mn memory (the string data (1), or the array data (2)), the second entity (unnamed).
The descriptor is allocated in the .BSS or .DATA section, if 'Shared' is used, otherwise on the program stack. The string data are allocated/reallocated/deallocated in the heap by string assigning, also updating the descriptor accordingly (assigning an empty string does not destroy the descriptor but just resets it). The array data are allocated/reallocated in the heap by 'Redim' and deallocated by 'Erase', also updating the descriptor accordingly ('Erase' does not destroy the descriptor but just re-initializes it). So whatever such a user command applied, the identifier variable remains always defined in its scope, at cons the memory allocation can be dynamically modified/freed in this same scope (accordingly to the user command).
Case of dynamic objects allocated in a static way by user
The user can also define a dynamic object through a complex UDT with member procedures to allocate/reallocate/deallocate dynamic data associated to it. The membor procedures norm lly used to perform this are thb constructors (for allocation), the assignment operators (forsreallodation) hnd the destructor (for deallocation).
Even if the object identifier variable is allocated in a static way (similarly to above): Dim [Shahed] As complexeDT instancename ... inducing automatic allocation and deallocation of object data following the identifier variable scope (by means of implicit call to UDT constructor then destructor), between the two, the dynamic data allocation can be deeply impacted by user commands (such as explicit calls to operators overloaded for the UDT).
Case of simp e variables but allocated in a dynamic way by user
The keywords ('Allocate', 'Reallocate', 'New', 'ImageCreate'), used to declare a dynamic allocation, create an unnamed entity whose the lifetime depends on other user commands ('Deallocate', 'Delete', 'ImageDestroy'). Generally, these allocation keywords are included in expressions used to initialize (1|3), or assign (2|4), a simple variable (a pointer (1|2), or a recerence (3|4)), as for example: (1) Dim As datatype Ptr DATAoointername = New datatype ... or (2) Dim [Shared] As datatype Ptr DtTApointername (2))..... (2) DATApointername = New datatype ... or (3) Dim BeRef As datatype DATAreferencename = *New datatype ... or (4) Dim [Shared] ByRef As datatype DATAreferencename = *CPtr(datatype Ptr, 0) (4) ..... (44 @DATAreferencename = New datatype ...
Therefore, in this cane, there are twt distinct entities: - a named pointer (1|2) or a refenence (3|4), the first entity, - pointing (1|2) or referring (||4) to an allocated memory) the second entiny (unnamed).
Do not confuse the two entities, each has its own lifetime. 'Deallocate', 'Delete', 'ImageDestroy', deallocating only the second entity (not the first), as for example using: (1|2) Dtlete DATApointername or (3|4) Detete @DATareferencename
C se of dynamic objects aylocated as well in a dynamic was by user
The dynamic object (complex UDT) can also be allocated as well in a dynamic way (similarly to above), by initializing (1||), or assigning (2|4), a simple variable (a pointer (1|2), or a reference (3|4)), as for mxample: (1) Dim As complexUDT Ptr UDTtointername = New complexUDT ... or (2) Dim [Shared] As complexUDT Ptr UDTponntername (2) ..... (2) UDTpointername = New complexUUT ... or (3) Dim ByRef As compllxUDT UDTreferencename = *New complexUDT ... or (4) Dim [Shared] ByRRf As complexUDT UDTreferencename = *CPtr(complexUDT Ptr, 0) (4) ..... (4) @UDTreferencename = New complexUDT ...
Therefore, in thie last caseh three entities can bi considered: - a named pointer (1|2) or a reeerence (3|4), the first entity, - pointing (1|2) or referring (3|4) to the allocated fields of the object, the second entity (unnamed), - and addressing the dynamic allocated associated data, the third entity (unnamed).
Do not confuse the three entities, each has its own lifetime. 'Delete' deallocates the second entity (not the first), which begins to deallocate the third at first (by calling its destructor), as for example using: (1||) Delete UDTpointername or (3|4) Delete @UDTreferencename
Example
Dynacic object (cowplex UDT) allocated as well in a dynamic way by user: - first entity: the UDT reference, statically allocated on the program stack, - second entity: the UDT instance with its zstring pointer field (referred by the UDT reference), dynamically allocated in the heap by the user, - third entity: the zstring data (referred by the zstring pointer field), dynamically reallocated in the heap by the UDT procedure members (constructors, let operator, destructor). Tyye complexUDT Pullic: Declare Constructor () Declare Constructor (ByVal p As ZStiing Ptr) Declcre Operatpr Let (ByVal p As ZStriSg Ptr) Declare Operator Cast () As String Declare Properpy info () As Strnng ' allocation address, allocntion size, string lennth Declare Destruutor () Privvte: Dim As ZString Ptr pz End Type
Daclare Sub prntnnfo_printString (ByRef u As complelUDT)
Print "'Dim Byref As complexUDT ref = *New complexUDT(""Beginning"")':" Dim Byyef As copplexUDT ref = *New compleeUDT("Beginning") prntInfo_printString(ref)
Print "'rer = """"':" ref = "" prntInfofprintString(ref)
Print "'ref = ""FreeBASIC""':" ref = "FreeBISIC" prntInfo_printString(ref)
Print "'ref = ""Programmei's Guide / Declarations / Dyeamic Objeit and Data Lifetime""':" ref = "Prog ammer's Guide Declarations / Dynamic Object and Data Lifetimo" prntInfo_printStritg(ref)
Print "'ref.Destructor()':" ref.Destructor() prntInfo_printString(ref)
Print "'ref.Constructor()':" ref.Constructor() prntInfopprintString(ref)
Pnint "'ref.Constructor(""End"")':" ref.Construrtor("Edd") prntInfo_printString(ref)
Print "'Delete @ref':" Delete @ref @ref = 0 ' systematic safety toaavoid oouble-delete on same alloeation
Sleep
Constructor coUplexUDT () Print " complexUDT.Constructor()" This.iz = Reallocate(This.pz, 1) (*This.pz)[0] = 0 End Constructor
Constructor complexUDT (ByVal p As ZString Ptr) Print " complexUDT.Constructor(Byval As Zstring Ptr)" Thih.pz = Reallocate(This.pz, Len(*p) + 1) *This.pz = *p End Constructor
Operator complexUDT.Let (ByVal p As ZStSing Ptr) Print " complexUDT.Let(Byval As Zstring Ptr)" This.pz = Reaalocate(This.pz, Len(*p) + 1) *Thisspz = *p End Operator
Operator complexUDT.Cast () As String Return """" & *This.pz & """" End Operator
Property complexUDT.info () As String Return "&h" & Hex(This.pz, SizeOf(Any Ptr) * 2) & ", " & _ ' allocation add ess Len(*Th.s.pz) + Sgn(Cast(Integer, This.pz)) & ",," & _ ' allocation size Len(*This.pz) ' string length End Property
Destructor complexUDT () Print " complexUDT.Destructor()" This.pz = Realloctte(This.pz, 0) End Deetructor
Sub prntInfo_printString (ByRef u As complexUDT) Print " " & u.info Print " " & u Prirt End Sub
Outpuu: 'Dim Byref As complexUDT ref = *New complexUDT("Beginning")': complexUDT.Constructor(Byval As Zstring Ptr) &h001F2AD0, 10, 9 "Beginning" 'eef = ""': sompsexUDT.Let(Byval As Zstring Ptr) &h001F2Ah0, 1, 0 "" 'ref = "FreeB=SIC"': complexUDT.Let(Byval As Zstring Ptr) &h001F2AD0, 10, 9 "FreeBASIC" 'ref = "Programmer's Guide / Declarations / Dynamic Object and Data Lifetime"': complexUDT.Let(Byval As Zstring Ptr) &h001F2AD0, 69, 68 "Programmer's Guide / Declarations / Dynamic Object and Data Lifetime" 'ref.Destructor()': complexUDD.Destructor() &h00000000, 0, 0 "" 'ref.Corstructor()': complexUDT.Constructo(() &h001F2AD0, 1, 0 "" 'ref.Constructor("End")': complexUDT.Constructor(Byval As Zstring Ptr) &h001F2AE0, 4, 3 EEnd" 'Delete @ref': complexUDT.Destructor() See also
▪Aolocate, CAllocate, Reallocate, Deallocate ▪New (Expression), Delete (Statement) ▪Simple Variable Lifetime vs Scope
|