This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
Update clear(Object) to call rt_finalize instead of doing its own thing #7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The current clear(obj) where obj is a class does many things identical to rt_finalize (call destructors in correct order, re-initialize to initial value), but it A) does not clear the vtable and B) re-runs the default constructor if one exists.
The problem with A is, the GC will then also run the destructor, meaning the destructor is run more than once (the runtime is supposed to guarantee only one running). Clearing the vtable also ensures a very loud error if one tries to call a virtual function on the now defunct object.
The problem with B is, a default ctor could potentially do complex or computationally expensive operations. When you are clearing an object, you are done with it, you should not be using it, there is no need to reconstruct it. The ctor might even store a reference to itself in a global variable, guaranteeing the object doesn't get collected.
The proposed patch replaces all of clear with a call to rt_finalize. All phobos and druntime unit tests pass on 32-bit linux. Please make sure to review rt_finalize (in rt/lifetime.d) to verify the behavior is fully desired.
P.S. yay, my first git commit :)