how to debug memory errors
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Nov 7 19:31:44 PST 2016
On 11/6/16 11:01 PM, thedeemon wrote:
> On Monday, 7 November 2016 at 02:22:35 UTC, Steven Schveighoffer wrote:
>> OP: it's not legal to destroy or even access GC allocated members in a
>> destructor. The GC may have already destroyed that data.
>
> Isn't destroy() fine there? It doesn't call destructors for already
> destroyed objects, so I guess it should be safe. (assuming the
> destructors don't do any allocations)
The problem is that you don't know when the GC has destroyed that
object. It may have been freed and already reallocated to something
else. The time between when your object becomes garbage and when it's
collected is not explicitly defined. Nor is the order of collection or
if it will even be collected at all.
Another possibility is that your object destroys something that is
pointed at by someone else. This is not a horrible condition, but could
result in some unwanted segfaults.
Hence, just don't access members from the destructor. You'll be glad you
didn't.
-Steve
More information about the Digitalmars-d-learn
mailing list