[Issue 4621] New: Destructors are inherently un- at safe
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Aug 11 11:08:34 PDT 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4621
Summary: Destructors are inherently un- at safe
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: normal
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: michel.fortin at michelf.com
--- Comment #0 from Michel Fortin <michel.fortin at michelf.com> 2010-08-11 14:08:33 EDT ---
Accessing the GC heap through a member in a destructors is inherently unsafe
because the GC might have already freed that memory. So destructors in SafeD
should not be able to access the GC-heap through a member. Here is an example:
@safe:
class C {
C other;
~this() {
writeln(other.toString()); // "other" might already have been freed.
}
}
void main() {
C c1 = new C;
C c2 = new C;
c1.other = c2;
c2.other = c1; // creating a circular reference
}
Given that the compiler has no way to know if a reference, pointer, or array
points to the GC heap or elsewhere, it might have to disallow any dereferencing
of any member and calls to functions that might dereference a member. And at
this point you can't do anything useful in a destructor, so you might just
disallow @safe destructors altogether.
Note that this applies to struct destructors too, since structs can be on the
heap (in their own memory block, part of an array, or as a member of a class).
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list