D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2497 - delete and null relationship needs more details
Summary: delete and null relationship needs more details
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dlang.org (show other issues)
Version: D2
Hardware: x86 All
: P2 normal
Assignee: No Owner
URL: http://www.digitalmars.com/d/2.0/expr...
Keywords: spec
Depends on:
Blocks:
 
Reported: 2008-12-06 14:04 UTC by Jerry Quinn
Modified: 2015-06-09 01:20 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jerry Quinn 2008-12-06 14:04:55 UTC
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 Stewart Gordon 2008-12-06 18:22:13 UTC
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 Jerry Quinn 2008-12-06 18:49:51 UTC
(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 Jarrett Billingsley 2008-12-06 19:00:11 UTC
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 Jerry Quinn 2008-12-06 23:23:41 UTC
(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.