Skip to content
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

Merged
1 commit merged into from
Feb 27, 2011

Conversation

schveiguy
Copy link
Member

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 :)

…e right things. This prevents dtors from running twice, and does not re-run the default ctor.
@andralex
Copy link
Member

Looking good, thanks. I'll pull this once I can unittest; right now the build is broken on OSX.

@andralex
Copy link
Member

Done -- thanks!

kinke referenced this pull request in kinke/druntime Feb 6, 2013
fix struct literal rvalue change
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants