forked from DlangRen/Programming-in-D
-
Notifications
You must be signed in to change notification settings - Fork 1
/
destroy.d
468 lines (367 loc) · 14.3 KB
/
destroy.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
130
131
132
133
134
135
136
137
138
139
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
169
170
171
172
173
174
175
176
177
178
179
180
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
Ddoc
$(DERS_BOLUMU $(CH4 destroy) and $(CH4 scoped))
$(P
We have seen the lifetimes of objects in the $(LINK2 /ders/d.en/lifetimes.html, Lifetimes and Fundamental Operations chapter).
)
$(P
In later chapters, we have seen that the objects are prepared for use in the constructor, which is called $(C this()); and the final operations of objects are applied in the destructor, which is called $(C ~this()).
)
$(P
For structs and other value types, the destructor is executed at the time when the lifetime of an object ends. For classes and other reference types, it is executed by the garbage collector some time in the future. The important distinction is that the destructor of a class object is not executed when its lifetime ends.
)
$(P
System resources are commonly returned back to the system in destructors. For example, $(C std.stdio.File) returns the file resource back to the operating system in its destructor. As it is not certain when the destructor of a class object will be called, the system resources that it holds may not be returned until too late when other objects cannot get a hold of the same resource.
)
$(H5 An example of calling destructors late)
$(P
Let's define a class to see the effects of executing class destructors late. The following constructor increments a $(C static) counter, and the destructor decrements it. As you remember, there is only one of each $(C static) member, which is shared by all of the objects of a type. Such a counter would indicate the number of objects that are yet to be destroyed.
)
---
$(CODE_NAME LifetimeObserved)class LifetimeObserved {
int[] array; // ← Belongs to each object
static size_t counter; // ← Shared by all objects
this() {
/* We are using a relatively large array to make each
* object consume a large amount of memory. Hopefully
* this will make the garbage collector call object
* destructors more frequently to open up space for
* more objects. */
array.length = 30_000;
/* Increment the counter for this object that is being
* constructed. */
++counter;
}
~this() {
/* Decrement the counter for this object that is being
* destroyed. */
--counter;
}
}
---
$(P
The following program constructs objects of that class inside a loop:
)
---
$(CODE_XREF LifetimeObserved)import std.stdio;
void main() {
foreach (i; 0 .. 20) {
auto variable = new LifetimeObserved; // ← start
write(LifetimeObserved.counter, ' ');
} // ← end
writeln();
}
---
$(P
The lifetime of each $(C LifetimeObserved) object is in fact very short: Its life starts when it is constructed by the $(C new) keyword and ends at the closing curly bracket of the $(C foreach) loop. Each object then becomes the responsibility of the garbage collector. The $(COMMENT start) and $(COMMENT end) comments indicate the start and end of the lifetimes.
)
$(P
Even though there is up to one object alive at a given time, the value of the counter indicates that the destructor is not executed when the lifetime ends:
)
$(SHELL
1 2 3 4 5 6 7 8 2 3 4 5 6 7 8 2 3 4 5 6
)
$(P
According to that output, the memory sweep algorithm of the garbage collector has delayed executing the destructor for up to 8 objects. ($(I Note: The output may be different depending on the garbage collection algorithm, available memory, and other factors.))
)
$(H5 $(IX destroy) $(IX destructor, execution) $(C destroy()) to execute the destructor)
$(P
$(C destroy()) executes the destructor for an object:
)
---
$(CODE_XREF LifetimeObserved)void main() {
foreach (i; 0 .. 20) {
auto variable = new LifetimeObserved;
write(LifetimeObserved.counter, ' ');
$(HILITE destroy(variable));
}
writeln();
}
---
$(P
Like before, the value of $(C LifetimeObserved.counter) is incremented by the constructor as a result of $(C new), and becomes 1. This time, right after it gets printed, $(C destroy()) executes the destructor for the object and the value of the counter is decremented again down to zero. For that reason, this time its value is always 1:
)
$(SHELL
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
)
$(P
Once destroyed, the object should be considered to be in an invalid state and must not be used anymore:
)
---
destroy(variable);
// ...
// Warning: Using a potentially invalid object
writeln(variable.array);
---
$(P
Although $(C destroy()) is primarily for reference types, it can also be called on $(C struct) objects to destroy them before the end of their normal lifetimes.
)
$(H5 When to use)
$(P
As has been seen in the previous example, $(C destroy()) is used when resources need to be released at a specific time without relying on the garbage collector.
)
$(H5 样例)
$(P
We had designed an $(C XmlElement) struct in the $(LINK2 /ders/d.en/special_functions.html, Constructor and Other Special Functions chapter). That struct was being used for printing XML elements in the format $(C <tag>value</tag>). Printing the closing tag has been the responsibility of the destructor:
)
---
struct XmlElement {
// ...
~this() {
writeln(indentation, "</", name, '>');
}
}
---
$(P
The following output was produced by a program that used that struct. This time, I am replacing the word "class" with "course" to avoid confusing it with the $(C class) keyword:
)
$(SHELL
<courses>
<course0>
<grade>
72
</grade> $(SHELL_NOTE The closing tags appear on correct lines)
<grade>
97
</grade> $(SHELL_NOTE)
<grade>
90
</grade> $(SHELL_NOTE)
</course0> $(SHELL_NOTE)
<course1>
<grade>
77
</grade> $(SHELL_NOTE)
<grade>
87
</grade> $(SHELL_NOTE)
<grade>
56
</grade> $(SHELL_NOTE)
</course1> $(SHELL_NOTE)
</courses> $(SHELL_NOTE)
)
$(P
The previous output happens to be correct because $(C XmlElement) is a $(C struct). The desired output is achieved simply by placing the objects in appropriate scopes:
)
---
void $(CODE_DONT_TEST)main() {
const $(HILITE courses) = XmlElement("courses", 0);
foreach (courseId; 0 .. 2) {
const courseTag = "course" ~ to!string(courseId);
const $(HILITE courseElement) = XmlElement(courseTag, 1);
foreach (i; 0 .. 3) {
const $(HILITE gradeElement) = XmlElement("grade", 2);
const randomGrade = uniform(50, 101);
writeln(indentationString(3), randomGrade);
} // ← gradeElement is destroyed
} // ← courseElement is destroyed
} // ← courses is destroyed
---
$(P
The destructor prints the closing tags as the objects gets destroyed.
)
$(P
To see how classes behave differently, let's convert $(C XmlElement) to a class:
)
---
import std.stdio;
import std.array;
import std.random;
import std.conv;
string indentationString(in int level) {
return replicate(" ", level * 2);
}
$(HILITE class) XmlElement {
string name;
string indentation;
this(in string name, in int level) {
this.name = name;
this.indentation = indentationString(level);
writeln(indentation, '<', name, '>');
}
~this() {
writeln(indentation, "</", name, '>');
}
}
void main() {
const courses = $(HILITE new) XmlElement("courses", 0);
foreach (courseId; 0 .. 2) {
const courseTag = "course" ~ to!string(courseId);
const courseElement = $(HILITE new) XmlElement(courseTag, 1);
foreach (i; 0 .. 3) {
const gradeElement = $(HILITE new) XmlElement("grade", 2);
const randomGrade = uniform(50, 101);
writeln(indentationString(3), randomGrade);
}
}
}
---
$(P
As the responsibility of calling the destructors are now left to the garbage collector, the program does not produce the desired output:
)
$(SHELL_SMALL
<courses>
<course0>
<grade>
57
<grade>
98
<grade>
87
<course1>
<grade>
84
<grade>
60
<grade>
99
</grade> $(SHELL_NOTE The closing tags appear at the end)
</grade> $(SHELL_NOTE)
</grade> $(SHELL_NOTE)
</course1> $(SHELL_NOTE)
</grade> $(SHELL_NOTE)
</grade> $(SHELL_NOTE)
</grade> $(SHELL_NOTE)
</course0> $(SHELL_NOTE)
</courses> $(SHELL_NOTE)
)
$(P
The destructor is still executed for every object but this time at the end when the program is exiting. ($(I Note: The garbage collector does not guarantee that the destructor will be called for every object. In reality, it is possible that there are no closing tags printed at all.))
)
$(P
$(C destroy()) ensures that the destructor is called at desired points in the program:
)
---
void $(CODE_DONT_TEST)main() {
const courses = new XmlElement("courses", 0);
foreach (courseId; 0 .. 2) {
const courseTag = "course" ~ to!string(courseId);
const courseElement = new XmlElement(courseTag, 1);
foreach (i; 0 .. 3) {
const gradeElement = new XmlElement("grade", 2);
const randomGrade = uniform(50, 101);
writeln(indentationString(3), randomGrade);
$(HILITE destroy(gradeElement));
}
$(HILITE destroy(courseElement));
}
$(HILITE destroy(courses));
}
---
$(P
With those changes, the output of the code now matches the output of the code that use structs:
)
$(SHELL
<courses>
<course0>
<grade>
66
</grade> $(SHELL_NOTE The closing tags appear on correct lines)
<grade>
75
</grade> $(SHELL_NOTE)
<grade>
68
</grade> $(SHELL_NOTE)
</course0> $(SHELL_NOTE)
<course1>
<grade>
73
</grade> $(SHELL_NOTE)
<grade>
62
</grade> $(SHELL_NOTE)
<grade>
100
</grade> $(SHELL_NOTE)
</course1> $(SHELL_NOTE)
</courses> $(SHELL_NOTE)
)
$(H5 $(IX scoped) $(C scoped()) to call the destructor automatically)
$(P
The program above has a weakness: The scopes may be exited before the $(C destroy()) lines are executed, commonly by thrown exceptions. If the $(C destroy()) lines must be executed even when exceptions are thrown, a solution is to take advantage of $(C scope()) and other features that we saw in the $(LINK2 /ders/d.en/exceptions.html, Exceptions chapter).
)
$(P
Another solution is to construct class objects by $(C std.typecons.scoped) instead of by the $(C new) keyword. $(C scoped()) wraps the class object inside a $(C struct) and the destructor of that $(C struct) object destroys the class object when itself goes out of scope.
)
$(P
The effect of $(C scoped()) is to make class objects behave similar to struct objects regarding lifetimes.
)
$(P
With the following changes, the program produces the expected output as before:
)
---
$(HILITE import std.typecons;)
// ...
void $(CODE_DONT_TEST)main() {
const courses = $(HILITE scoped!)XmlElement("courses", 0);
foreach (courseId; 0 .. 2) {
const courseTag = "course" ~ to!string(courseId);
const courseElement = $(HILITE scoped!)XmlElement(courseTag, 1);
foreach (i; 0 .. 3) {
const gradeElement = $(HILITE scoped!)XmlElement("grade", 2);
const randomGrade = uniform(50, 101);
writeln(indentationString(3), randomGrade);
}
}
}
---
$(P
Note that there are no $(C destroy()) lines anymore.
)
$(P
$(IX proxy) $(IX RAII) $(C scoped()) is a function that returns a special $(C struct) object encapsulating the actual $(C class) object. The returned object acts as a proxy to the encapsulated one. (In fact, the type of $(C courses) above is $(C Scoped), not $(C XmlElement).)
)
$(P
When the destructor of the $(C struct) object is called automatically as its lifetime ends, it calls $(C destroy()) on the $(C class) object that it encapsulates. (This is an application of the $(I Resource Acquisition Is Initialization) (RAII) idiom. $(C scoped()) achieves this by the help of $(LINK2 /ders/d.en/templates.html, templates) and $(LINK2 /ders/d.en/alias_this.html, $(C alias this)), both of which we will see in later chapters.)
)
$(P
It is desirable for a proxy object to be used as conveniently as possible. In fact, the object that $(C scoped()) returns can be used exactly like the actual $(C class) type. For example, the member functions of the actual type can be called on it:
)
---
import std.typecons;
class C {
void foo() {
}
}
void main() {
auto p = scoped!C();
p$(HILITE .foo()); // Proxy object p is being used as type C
}
---
$(P
However, that convenience comes with a price: The proxy object may hand out a reference to the actual object right before destroying it. This can happen when the actual $(C class) type is specified explicitly on the left hand-side:
)
---
$(HILITE C) c = scoped!C(); $(CODE_NOTE_WRONG BUG)
c.foo(); $(CODE_NOTE_WRONG Accesses a destroyed object)
---
$(P
In that definition, $(C c) is not the proxy object; rather, as defined by the programmer, a $(C class) variable referencing the encapsulated object. Unfortunately, the proxy object that is constructed on the right-hand side gets terminated at the end of the expression that constructs it. As a result, using $(C c) in the program would be an error, likely causing a runtime error:
)
$(SHELL
Segmentation fault
)
$(P
For that reason, do not define $(C scoped()) variables by the actual type:
)
---
$(HILITE C) a = scoped!C(); $(CODE_NOTE_WRONG BUG)
auto b = scoped!C(); $(CODE_NOTE correct)
const c = scoped!C(); $(CODE_NOTE correct)
immutable d = scoped!C(); $(CODE_NOTE correct)
---
$(H5 摘要)
$(UL
$(LI
$(C destroy()) is for executing the destructor of a class object explicitly.
)
$(LI
Objects that are constructed by $(C scoped()) are destroyed upon leaving their respective scopes.
)
$(LI It is a bug to define $(C scoped()) variables by the actual type.)
)
Macros:
SUBTITLE=destroy and scoped
DESCRIPTION=destroy() to call destructors explicitly on class objects and std.typecons.scoped to have the destructors executed on class objects automatically.
KEYWORDS=d programming language tutorial book destroy scoped