GC.inFinalizer

Queries the GC whether the current thread is running object finalization as part of a GC collection, or an explicit call to runFinalizers.

As some GC implementations (such as the current conservative one) don't support GC memory allocation during object finalization, this function can be used to guard against such programming errors.

struct GC
extern (C) pragma(mangle, "gc_inFinalizer") static nothrow @nogc @safe
bool
inFinalizer
()

Return Value

Type: bool

true if the current thread is in a finalizer, a destructor invoked by the GC.

Examples

// Only code called from a destructor is executed during finalization.
assert(!GC.inFinalizer);
1 enum Outcome
2 {
3     notCalled,
4     calledManually,
5     calledFromDruntime
6 }
7 
8 static class Resource
9 {
10     static Outcome outcome;
11 
12     this()
13     {
14         outcome = Outcome.notCalled;
15     }
16 
17     ~this()
18     {
19         if (GC.inFinalizer)
20         {
21             outcome = Outcome.calledFromDruntime;
22 
23             import core.exception : InvalidMemoryOperationError;
24             try
25             {
26                 /*
27                  * Presently, allocating GC memory during finalization
28                  * is forbidden and leads to
29                  * `InvalidMemoryOperationError` being thrown.
30                  *
31                  * `GC.inFinalizer` can be used to guard against
32                  * programming erros such as these and is also a more
33                  * efficient way to verify whether a destructor was
34                  * invoked by the GC.
35                  */
36                 cast(void) GC.malloc(1);
37                 assert(false);
38             }
39             catch (InvalidMemoryOperationError e)
40             {
41                 return;
42             }
43             assert(false);
44         }
45         else
46             outcome = Outcome.calledManually;
47     }
48 }
49 
50 static void createGarbage()
51 {
52     auto r = new Resource;
53     r = null;
54 }
55 
56 assert(Resource.outcome == Outcome.notCalled);
57 createGarbage();
58 GC.collect;
59 assert(
60     Resource.outcome == Outcome.notCalled ||
61     Resource.outcome == Outcome.calledFromDruntime);
62 
63 auto r = new Resource;
64 GC.runFinalizers((cast(const void*)typeid(Resource).destructor)[0..1]);
65 assert(Resource.outcome == Outcome.calledFromDruntime);
66 Resource.outcome = Outcome.notCalled;
67 
68 debug(MEMSTOMP) {} else
69 {
70     // assume Resource data is still available
71     r.destroy;
72     assert(Resource.outcome == Outcome.notCalled);
73 }
74 
75 r = new Resource;
76 assert(Resource.outcome == Outcome.notCalled);
77 r.destroy;
78 assert(Resource.outcome == Outcome.calledManually);
Suggestion Box / Bug Report