Bug 2497 – delete and null relationship needs more details

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dlang.org
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2008-12-06T14:04:00Z
Last change time
2015-06-09T01:20:45Z
Keywords
spec
Assigned to
nobody
Creator
jlquinn

Comments

Comment #0 by jlquinn — 2008-12-06T14:04:55Z
1) A a = null; delete a; is this OK or not? 2) void do(A b) { delete b; } A a = new A; do(a); if (a is null) { writefln("yes"); } does a get reset to null or not?
Comment #1 by smjg — 2008-12-06T18:22:13Z
1. Good question. 2. No, because do has no access to variable a. However, it were declared as ref, it would. It does say "The pointer, dynamic array, or reference is set to null after the delete is performed." Notice the singular.
Comment #2 by jlquinn — 2008-12-06T18:49:51Z
(In reply to comment #1) > 2. No, because do has no access to variable a. However, it were declared as > ref, it would. OK, this is what I thought. What happens when you access a, then? In Java you'd get an NPE. no matter how you try to access the object. Does it coredump or is there a defined behavior?
Comment #3 by jarrett.billingsley — 2008-12-06T19:00:11Z
It's undefined. Comparing to Java doesn't make much sense here, since the only way an object can go away in Java is if no references refer to it, so there's no way to have a reference to an object that has been collected. delete is sort of a "power user" tool for when you _know_ that there are no other references to the object and want to help out the GC a bit, so it's going to have sharp edges.
Comment #4 by jlquinn — 2008-12-06T23:23:41Z
(In reply to comment #3) > It's undefined. Comparing to Java doesn't make much sense here, since the only > way an object can go away in Java is if no references refer to it, so there's > no way to have a reference to an object that has been collected. Fair enough. It would be useful to note that accessing a deleted class through the reference is undefined. Otherwise it might leave others guessing.
Comment #5 by github-bugzilla — 2012-01-22T21:03:54Z