4 ms·
That's kinda the point: you can't skip it with RAII, unless you avoid exiting a scope (longjmp?). I suppose you can have two versions of the class, and instant
by ori_b 3y ago
That's kinda the point: you can't skip it with RAII, unless you avoid exiting a scope (longjmp?).
I suppose you can have two versions of the class, and instantiate the leaky one only if you have GC, but that seems like work that the GC should free you from. It seems better to optimize the memory allocations, perhaps with an arena.
- layer8 3y agoNot sure what you mean. My point is, for the case of no GC, you have to provide a destructor, even if the work done by the destructor only consists of freeing memory. With GC, calling the destructor could be omitted if it only frees memory. Client code however doesn’t know what a destructor does internally, so always has to call it even under a GC. The implementation of the destructor, however, can check whether it is running under a GC, and can then skip the freeing. Edit due to your edit: An arena allocator isn’t always convenient, for example when you have multiple objects with different lifetimes that move and/or partially share internal elements between them, so the element lifetimes are largely orthogonal to the containing object’s lifetimes.
- PaulDavisThe1st 3y ago> My point is, for the case of no GC, you have to provide a destructor, even if the work done by the destructor only consists of freeing memory. for the case of no GC, a destructor must be provided. That's an important difference.