management operators: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | ## Management operators feature | |
| 2 | 2 | ||
| 3 | - | ||
| 3 | + | From Free Pascal version 3.1.1 onwards, there is a new language feature called management operators for extended or advanced records. | |
| 4 | 4 | ||
| 5 | - | ||
| 5 | + | The new operators are: Initialize, Finalize, AddRef and Copy. | |
| 6 | 6 | ||
| 7 | - | ||
| 7 | + | These are a fairly unique feature, and are called "management operators" because: | |
| 8 | 8 | ||
| 9 | - | ||
| 10 | - | ||
| 9 | + | * Each record, even non-managed or empty, that implements any management operators becomes a managed type. | |
| 10 | + | * They make it possible to implement new custom types with their own memory management, e.g: new string types, fast TValue implementations without hacks on the RTL, etc. | |
| 11 | 11 | ||
| 12 | - | ||
| 12 | + | Management operators have no result type as opposed to normal operators, and work via a very simple VMT. | |
| 13 | 13 | ||
| 14 | - | ||
| 14 | + | Thanks to this, it is possible to combine management operators with all low-level RTL functions, such as InitializeArray / FinalizeArray / etc. | |
| 15 | 15 | ||
| 16 | - | ||
| 16 | + | Management operators can be used for many things: | |
| 17 | 17 | ||
| 18 | - | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 24 | - | ||
| 18 | + | * More granularly controlling the lifetimes of simple value types / primitives | |
| 19 | + | * Implementing "nullable" value types | |
| 20 | + | * Custom ARC implementations | |
| 21 | + | * A very fast RTTI.TValue implementation | |
| 22 | + | * As a replacement for manually-called Init/Done record methods like the popular "mORMot" library uses for many types (for example in SynCommons.TSynLocker). | |
| 23 | + | * Auto init/finit for pointers/classes/simple types or anything else we have in Pascal. | |
| 24 | + | * Much more | |
| 25 | 25 | ||
| 26 | - | ||
| 26 | + | They work correctly in all possible ways with the RTL: | |
| 27 | 27 | ||
| 28 | - | ||
| 29 | - | ||
| 30 | - | ||
| 31 | - | ||
| 32 | - | ||
| 33 | - | ||
| 34 | - | ||
| 35 | - | ||
| 36 | - | ||
| 28 | + | * New (Initialize). | |
| 29 | + | * Dispose (Finalize). | |
| 30 | + | * Initialize (Initialize). | |
| 31 | + | * Finalize (Finalize). | |
| 32 | + | * InitializeArray (Initialize). | |
| 33 | + | * FinalizeArray (Finalize). | |
| 34 | + | * SetLength (Initialize/Finalize). | |
| 35 | + | * Copy (AddRef). | |
| 36 | + | * RTTI.IsManaged. | |
| 37 | 37 | ||
| 38 | - | ||
| 38 | + | Management operators, when implemented, are called implicitly at various times. For example: | |
| 39 | 39 | ||
| 40 | - | ||
| 41 | - | ||
| 42 | - | ||
| 43 | - | ||
| 44 | - | ||
| 40 | + | * Global variables (Initialize/Finalize). | |
| 41 | + | * Local variables (Initialize/Finalize). | |
| 42 | + | * For fields inside records, objects or classes (Initialize/Finalize). | |
| 43 | + | * Variable assignment (Copy). | |
| 44 | + | * For parameters for routines - AddRef/Finalize/none - this depends on modifiers like var / constref / const. | |
| 45 | 45 | ||
| 46 | 46 | ## Initialize | |
| 47 | 47 | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - | ||
| 48 | + | The initialize operator is called directly after stack (or heap) memory allocation for a record happens. | |
| 51 | 49 | ||
| 52 | - | ||
| 50 | + | It allows for custom automatic initialization code. | |
| 51 | + | ``` | |
| 53 | 52 | program TestInitialize; | |
| 54 | - | ||
| 55 | - | ||
| 56 | - | ||
| 57 | - | ||
| 58 | - | ||
| 59 | - | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 63 | - | ||
| 64 | - | ||
| 65 | - | ||
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 69 | - | ||
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 73 | - | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 53 | + | ||
| 54 | + | {$if FPC_FULLVERSION < 30101} | |
| 55 | + | {$ERROR this demo needs version 3.1.1} | |
| 56 | + | {$endif} | |
| 57 | + | {$mode delphi} | |
| 58 | + | ||
| 59 | + | type | |
| 60 | + | PRec = ^TRec; | |
| 61 | + | ||
| 62 | + | TRec = record | |
| 63 | + | I: Integer; | |
| 64 | + | class operator Initialize(var aRec: TRec); | |
| 65 | + | end; | |
| 66 | + | ||
| 67 | + | class operator TRec.Initialize(var aRec: TRec); | |
| 68 | + | begin | |
| 69 | + | aRec.I := 0; | |
| 70 | + | end; | |
| 71 | + | ||
| 72 | + | procedure PrintTRec(r: PRec); | |
| 73 | + | begin | |
| 74 | + | WriteLn('Initialized TRec field i: ', r^.I); // should always be zero, stack or heap | |
| 75 | + | end; | |
| 76 | + | ||
| 77 | + | var | |
| 78 | + | a, b: PRec; | |
| 79 | + | ||
| 80 | + | begin | |
| 81 | + | New(a); | |
| 82 | + | New(b); // standard "new" does not initialize, but now it does! | |
| 83 | + | PrintTRec(a); | |
| 84 | + | PrintTRec(b); | |
| 85 | + | Dispose(a); | |
| 86 | + | Dispose(b); | |
| 87 | + | end. | |
| 88 | + | ``` | |
| 77 | 89 | ||
| 78 | - | ||
| 79 | - | ||
| 90 | + | ||
| 80 | 91 | ||
| 81 | - | ||
| 82 | - | ||
| 83 | - | ||
| 84 | - | ||
| 85 | - | ||
| 86 | - | ||
| 87 | - | ||
| 88 | - | ||
| 92 | + | Warning: Do not use the Default() intrinsic inside the initialize operator because that causes initialize to be called twice. Use other methods to initialize a record, such as fillchar. | |
| 93 | + | An example on how to initialize properly without calling Default() or system.initialize is: | |
| 89 | 94 | ``` | |
| 95 | + | {$mode objfpc}{$modeswitch advancedrecords} | |
| 96 | + | type | |
| 97 | + | array10 = array[0..9] of byte; | |
| 98 | + | ||
| 99 | + | TTest = record | |
| 100 | + | fields:array10; | |
| 101 | + | class operator initialize(var value:TTest); | |
| 102 | + | end; | |
| 103 | + | ||
| 104 | + | class operator ttest.initialize(var value:TTest); | |
| 105 | + | begin | |
| 106 | + | // sizeof takes only the fields of the record in to account | |
| 107 | + | fillbyte(value,sizeof(TTest),0);// do not use default(), that triggers initialize twice | |
| 108 | + | end; | |
| 109 | + | ||
| 110 | + | procedure testme; | |
| 111 | + | var | |
| 112 | + | a,b:TTest;// stack | |
| 113 | + | i:integer; | |
| 114 | + | begin | |
| 115 | + | a.fields:=[1,2,3,4,5,6,7,8,9,10]; | |
| 116 | + | a:=b; | |
| 117 | + | for i :=0 to 9 do | |
| 118 | + | write(a.fields[i]:3); | |
| 119 | + | end; | |
| 120 | + | ||
| 121 | + | begin | |
| 122 | + | testme; | |
| 123 | + | end. | |
| 124 | + | ``` | |
| 90 | 125 | ||
| 91 | 126 | ## Finalize | |
| 92 | 127 | ||
| 93 | - | ||
| 94 | - | ||
| 95 | - | ||
| 128 | + | Finalize is called when a record goes out of scope. | |
| 96 | 129 | ||
| 97 | - | ||
| 130 | + | It is useful for automatic custom finalization code. | |
| 131 | + | ``` | |
| 98 | 132 | program TestFinalize; | |
| 99 | - | ||
| 100 | - | ||
| 101 | - | ||
| 102 | - | ||
| 103 | - | ||
| 104 | - | ||
| 105 | - | ||
| 106 | - | ||
| 107 | - | ||
| 108 | - | ||
| 109 | - | ||
| 110 | - | ||
| 111 | - | ||
| 112 | - | ||
| 113 | - | ||
| 114 | - | ||
| 115 | - | ||
| 116 | - | ||
| 117 | - | ||
| 118 | - | ||
| 119 | - | ||
| 120 | - | ||
| 121 | - | ||
| 122 | - | ||
| 123 | - | ||
| 124 | - | ||
| 125 | - | ||
| 126 | - | ||
| 127 | - | ||
| 128 | - | ||
| 129 | - | ||
| 133 | + | ||
| 134 | + | {$if FPC_FULLVERSION < 30101} | |
| 135 | + | {$ERROR this demo needs version 3.1.1} | |
| 136 | + | {$endif} | |
| 137 | + | {$mode delphi} | |
| 138 | + | ||
| 139 | + | type | |
| 140 | + | PRec = ^TRec; | |
| 141 | + | ||
| 142 | + | TRec = record | |
| 143 | + | I: Integer; | |
| 144 | + | class operator Finalize(var aRec: TRec); | |
| 145 | + | end; | |
| 146 | + | ||
| 147 | + | class operator TRec.Finalize(var aRec: TRec); | |
| 148 | + | begin | |
| 149 | + | WriteLn('Just to let you know: I am finalizing..'); | |
| 150 | + | end; | |
| 151 | + | ||
| 152 | + | var | |
| 153 | + | a, b: PRec; | |
| 154 | + | c: array of TRec; | |
| 155 | + | ||
| 156 | + | begin | |
| 157 | + | New(a); | |
| 158 | + | New(b); | |
| 159 | + | Dispose(a); | |
| 160 | + | Dispose(b); | |
| 161 | + | WriteLn('Just before program termination this will also be finalized'); | |
| 162 | + | SetLength(c, 4); | |
| 163 | + | end. | |
| 130 | 164 | ``` | |
| 131 | 165 | ||
| 132 | 166 | ## AddRef | |
| 133 | 167 | ||
| 134 | - | ||
| 135 | - | ||
| 136 | - | ||
| 168 | + | AddRef is called after the contents of a record have been duplicated by a bitwise copy (for example **after** , not during, an assigment.) | |
| 137 | 169 | ||
| 138 | - | ||
| 170 | + | By itself it does not do any lifetime management, but you can use it to implement it. See also Copy. | |
| 171 | + | ``` | |
| 139 | 172 | program TestAddref; | |
| 140 | - | ||
| 141 | - | ||
| 142 | - | ||
| 143 | - | ||
| 144 | - | ||
| 145 | - | ||
| 146 | - | ||
| 147 | - | ||
| 148 | - | ||
| 149 | - | ||
| 150 | - | ||
| 151 | - | ||
| 152 | - | ||
| 153 | - | ||
| 154 | - | ||
| 155 | - | ||
| 156 | - | ||
| 157 | - | ||
| 158 | - | ||
| 159 | - | ||
| 160 | - | ||
| 161 | - | ||
| 162 | - | ||
| 163 | - | ||
| 164 | - | ||
| 165 | - | ||
| 166 | - | ||
| 167 | - | ||
| 168 | - | ||
| 173 | + | ||
| 174 | + | {$if FPC_FULLVERSION < 30101} | |
| 175 | + | {$ERROR this demo needs version 3.1.1} | |
| 176 | + | {$endif} | |
| 177 | + | {$mode delphi} | |
| 178 | + | ||
| 179 | + | uses | |
| 180 | + | SysUtils; | |
| 181 | + | ||
| 182 | + | type | |
| 183 | + | PRec = ^TRec; | |
| 184 | + | ||
| 185 | + | TRec = record | |
| 186 | + | I: Integer; | |
| 187 | + | class operator AddRef(var aRec: TRec); | |
| 188 | + | end; | |
| 189 | + | ||
| 190 | + | class operator TRec.AddRef(var aRec: TRec); | |
| 191 | + | begin | |
| 192 | + | WriteLn('Just to let you know: maybe you can do lifetime management here..'); | |
| 193 | + | end; | |
| 194 | + | ||
| 195 | + | var | |
| 196 | + | a, b: array of TRec; | |
| 197 | + | ||
| 198 | + | begin | |
| 199 | + | SetLength(a, 4); | |
| 200 | + | b := Copy(a); | |
| 201 | + | end. | |
| 169 | 202 | ``` | |
| 170 | 203 | ||
| 171 | 204 | ## Copy | |
| 172 | 205 | ||
| 173 | - | ||
| 174 | - | ||
| 175 | - | ||
| 176 | - | ||
| 177 | - | ||
| 206 | + | The Copy operator, if implemented, is called instead of the default copy behavior. This operator is responsible for copying everything that's needed from the source to the target. | |
| 178 | 207 | ||
| 179 | - | ||
| 208 | + | This is very helpfull if your record contains e.g. a dynamic array. | |
| 209 | + | ||
| 210 | + | Consider the following program: | |
| 211 | + | ``` | |
| 180 | 212 | uses | |
| 181 | - | ||
| 182 | - | ||
| 183 | - | ||
| 184 | - | ||
| 185 | - | ||
| 186 | - | ||
| 187 | - | ||
| 188 | - | ||
| 189 | - | ||
| 190 | - | ||
| 191 | - | ||
| 192 | - | ||
| 193 | - | ||
| 194 | - | ||
| 195 | - | ||
| 196 | - | ||
| 197 | - | ||
| 198 | - | ||
| 199 | - | ||
| 200 | - | ||
| 201 | - | ||
| 202 | - | ||
| 203 | - | ||
| 204 | - | ||
| 205 | - | ||
| 206 | - | ||
| 207 | - | ||
| 208 | - | ||
| 209 | - | ||
| 210 | - | ||
| 211 | - | ||
| 212 | - | ||
| 213 | + | SysUtils; | |
| 214 | + | ||
| 215 | + | type | |
| 216 | + | TArr = array of integer; | |
| 217 | + | TRec = record | |
| 218 | + | Arr: TArr; | |
| 219 | + | end; | |
| 220 | + | ||
| 221 | + | implementation | |
| 222 | + | ||
| 223 | + | function ArrToString(Arr: TArr): String; | |
| 224 | + | var | |
| 225 | + | X: Integer; | |
| 226 | + | begin | |
| 227 | + | Result := ''; | |
| 228 | + | for X in Arr do Result := Result + IntToStr(X) + ','; | |
| 229 | + | if Result <> '' then Delete(Result, Length(Result), 1); | |
| 230 | + | Result := '[' + Result + ']'; | |
| 231 | + | end; | |
| 232 | + | ||
| 233 | + | var | |
| 234 | + | R1, R2: TRec; | |
| 235 | + | ||
| 236 | + | begin | |
| 237 | + | writeln('Normal record type with a dynamic array'); | |
| 238 | + | R1.Arr := [1,2,3]; | |
| 239 | + | R2 := R1; | |
| 240 | + | writeln('R1.Arr = ',ArrToString(R1.Arr)); //[1,2,3] | |
| 241 | + | writeln('R2.Arr = ',ArrToString(R2.Arr)); //[1,2,3] | |
| 242 | + | writeln('After R2.Arr[0] := 666'); | |
| 243 | + | R2.Arr[0] := 666; | |
| 244 | + | writeln('R1.Arr = ',ArrToString(R1.Arr)); //[666,2,3] | |
| 245 | + | writeln('R2.Arr = ',ArrToString(R2.Arr)); //[666,2,3] | |
| 246 | + | end. | |
| 213 | 247 | ``` | |
| 214 | 248 | ||
| 215 | - | ||
| 216 | - | ||
| 217 | - | ||
| 249 | + | This will output: | |
| 250 | + | ``` | |
| 251 | + | Normal record type with a dynamic array | |
| 218 | 252 | R1.Arr = [1,2,3] | |
| 219 | 253 | R2.Arr = [1,2,3] | |
| 220 | - | ||
| 254 | + | After R2.Arr[0] := 666 | |
| 221 | 255 | R1.Arr = [666,2,3] | |
| 222 | 256 | R2.Arr = [666,2,3] | |
| 223 | - | ||
| 224 | - | ||
| 225 | - | ||
| 226 | - | ||
| 257 | + | ``` | |
| 227 | 258 | ||
| 228 | - | ||
| 259 | + | Notice that changing `R2.Arr` changes `R1.Arr` as well, since both the arrays point to the same memory location. | |
| 260 | + | When you do `R2 := R1`, you do not copy the contents of R1.Arr into R2, you copy the pointer that points to the first element of R1.Arr. | |
| 261 | + | If you want to avoid this, you could write a workaround like: | |
| 262 | + | ``` | |
| 229 | 263 | function CopyFrom(Src: TRec): TRec; | |
| 230 | - | ||
| 231 | - | ||
| 232 | - | ||
| 264 | + | begin | |
| 265 | + | Result.Arr := Copy(Src.Arr); | |
| 266 | + | end; | |
| 233 | 267 | ``` | |
| 234 | 268 | ||
| 235 | - | ||
| 236 | - | ||
| 237 | - | ||
| 238 | - | ||
| 239 | - | ||
| 269 | + | And then always use `R2 := CopyFrom(R1)` instead of `R2 := R1`. | |
| 270 | + | ||
| 271 | + | Or you can solve this problem using the Copy management operator: | |
| 272 | + | ``` | |
| 240 | 273 | {$modeswitch advancedrecords} | |
| 241 | - | ||
| 242 | - | ||
| 243 | - | ||
| 244 | - | ||
| 245 | - | ||
| 246 | - | ||
| 247 | - | ||
| 248 | - | ||
| 249 | - | ||
| 250 | - | ||
| 251 | - | ||
| 252 | - | ||
| 253 | - | ||
| 254 | - | ||
| 255 | - | ||
| 256 | - | ||
| 257 | - | ||
| 258 | - | ||
| 259 | - | ||
| 260 | - | ||
| 261 | - | ||
| 262 | - | ||
| 263 | - | ||
| 264 | - | ||
| 265 | - | ||
| 266 | - | ||
| 267 | - | ||
| 268 | - | ||
| 269 | - | ||
| 270 | - | ||
| 271 | - | ||
| 272 | - | ||
| 273 | - | ||
| 274 | - | ||
| 275 | - | ||
| 276 | - | ||
| 277 | - | ||
| 278 | - | ||
| 274 | + | uses | |
| 275 | + | SysUtils; | |
| 276 | + | ||
| 277 | + | type | |
| 278 | + | TArr = array of integer; | |
| 279 | + | TManagedRec = record | |
| 280 | + | Arr: TArr; | |
| 281 | + | class operator Copy(constref aSrc: TManagedRec; var aDst: TManagedRec); | |
| 282 | + | end; | |
| 283 | + | implementation | |
| 284 | + | { TMangedRec } | |
| 285 | + | class operator TManagedRec.Copy(constref aSrc: TManagedRec; var aDst: TManagedRec); | |
| 286 | + | begin | |
| 287 | + | aDst.Arr := Copy(aSrc.Arr); | |
| 288 | + | end; | |
| 289 | + | ||
| 290 | + | function ArrToString(Arr: TArr): String; | |
| 291 | + | var | |
| 292 | + | X: Integer; | |
| 293 | + | begin | |
| 294 | + | Result := ''; | |
| 295 | + | for X in Arr do Result := Result + IntToStr(X) + ','; | |
| 296 | + | if Result <> '' then Delete(Result, Length(Result), 1); | |
| 297 | + | Result := '[' + Result + ']'; | |
| 298 | + | end; | |
| 299 | + | ||
| 300 | + | var | |
| 301 | + | M1, M2: TManagedRec; | |
| 302 | + | begin | |
| 303 | + | writeln('Managed record type with a dynamic array and class operator Copy'); | |
| 304 | + | M1.Arr := [1,2,3]; | |
| 305 | + | M2 := M1; | |
| 306 | + | writeln('M1.Arr = ',ArrToString(M1.Arr)); //[1,2,3] | |
| 307 | + | writeln('M2.Arr = ',ArrToString(M2.Arr)); //[1,2,3] | |
| 308 | + | writeln('After M2.Arr[0] := 666'); | |
| 309 | + | M2.Arr[0] := 666; | |
| 310 | + | writeln('M1.Arr = ',ArrToString(M1.Arr)); //[1,2,3] | |
| 311 | + | writeln('M2.Arr = ',ArrToString(M2.Arr)); //[666,2,3] | |
| 312 | + | end. | |
| 279 | 313 | ``` | |
| 280 | 314 | ||
| 281 | - | ||
| 282 | - | ||
| 283 | - | ||
| 315 | + | This will output: | |
| 316 | + | ``` | |
| 317 | + | Managed record type with a dynamic array and class operator Copy | |
| 284 | 318 | M1.Arr = [1,2,3] | |
| 285 | 319 | M2.Arr = [1,2,3] | |
| 286 | - | ||
| 320 | + | After M2.Arr[0] := 666 | |
| 287 | 321 | M1.Arr = [1,2,3] | |
| 288 | 322 | M2.Arr = [666,2,3] | |
| 323 | + | ``` | |
| 289 | 324 | ||
| 290 | - | ||
| 325 | + | There is also a (more complex) example in [[1]](<https://gitlab.com/freepascal.org/fpc/source/-/blob/main/tests/test/tmoperator8.pp>) within the FPC sources. | |
| 291 | 326 | ||
| 292 | 327 | ## Example of using Initialize and Finalize | |
| 293 | - | ||
| 294 | - | ||
| 328 | + | ``` | |
| 295 | 329 | unit UResourceHandlers; | |
| 296 | - | ||
| 297 | - | ||
| 298 | - | ||
| 299 | - | ||
| 300 | - | ||
| 301 | - | ||
| 302 | - | ||
| 303 | - | ||
| 304 | - | ||
| 305 | - | ||
| 306 | - | ||
| 307 | - | ||
| 308 | - | ||
| 309 | - | ||
| 310 | - | ||
| 311 | - | ||
| 312 | - | ||
| 313 | - | ||
| 314 | - | ||
| 315 | - | ||
| 316 | - | ||
| 317 | - | ||
| 318 | - | ||
| 319 | - | ||
| 320 | - | ||
| 321 | - | ||
| 322 | - | ||
| 323 | - | ||
| 324 | - | ||
| 325 | - | ||
| 326 | - | ||
| 327 | - | ||
| 328 | - | ||
| 329 | - | ||
| 330 | - | ||
| 331 | - | ||
| 330 | + | ||
| 331 | + | {$if FPC_FULLVERSION < 30101} | |
| 332 | + | {$ERROR this demo needs version 3.1.1} | |
| 333 | + | {$endif} | |
| 334 | + | {$mode delphi} | |
| 335 | + | ||
| 336 | + | interface | |
| 337 | + | ||
| 338 | + | uses | |
| 339 | + | Classes, SysUtils; | |
| 340 | + | ||
| 341 | + | type | |
| 342 | + | ||
| 343 | + | { TObjectHandler } | |
| 344 | + | ||
| 345 | + | TObjectHandler = record | |
| 346 | + | obj: TObject; | |
| 347 | + | class operator Initialize(var hdl: TObjectHandler); | |
| 348 | + | class operator Finalize(var hdl: TObjectHandler); | |
| 349 | + | end; | |
| 350 | + | ||
| 351 | + | implementation | |
| 352 | + | ||
| 353 | + | { TObjectHandler } | |
| 354 | + | ||
| 355 | + | class operator TObjectHandler.Initialize(var hdl: TObjectHandler); | |
| 356 | + | begin | |
| 357 | + | hdl.obj := nil; | |
| 358 | + | end; | |
| 359 | + | ||
| 360 | + | class operator TObjectHandler.finalize(var hdl: TObjectHandler); | |
| 361 | + | begin | |
| 362 | + | FreeAndNil(hdl.obj); | |
| 363 | + | end; | |
| 364 | + | ||
| 365 | + | end. | |
| 332 | 366 | ``` | |
| 333 | 367 | ||
| 334 | - | ||
| 335 | - | ||
| 336 | - | ||
| 368 | + | How to use it | |
| 369 | + | ``` | |
| 337 | 370 | procedure ExtractionResultTests.ObjectHandlerTest; | |
| 338 | - | ||
| 339 | - | ||
| 340 | - | ||
| 341 | - | ||
| 342 | - | ||
| 343 | - | ||
| 344 | - | ||
| 371 | + | var | |
| 372 | + | a: TRow; | |
| 373 | + | ah: TObjectHandler; | |
| 374 | + | begin | |
| 375 | + | a := TRow.Create; | |
| 376 | + | ah.obj := a; | |
| 377 | + | end; | |
| 345 | 378 | ``` | |
| 346 | 379 | ||
| 347 | 380 | In this case the destructor of the TRow object is called when the handler goes out of scope. The same idea could be used for other resources like TMutex / TCriticalSection / anything else along those lines. | |
| 348 | 381 | ||
| 349 | 382 | --- | |
| 350 | - | ||
| 351 | - | ||
| 383 | + | *From the Free Pascal wiki, page "management operators", as the Internet Archive kept it on 2025-12-12, last changed on 2025-03-02. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
