GC does not delete subclass
Steven Schveighoffer
schveiguy at yahoo.com
Tue Dec 18 11:02:19 PST 2007
"Jason House" wrote
> Steven Schveighoffer Wrote:
>
>> "Jason House" wrote
>> > I haven't tried this, but is it possible to declare class member
>> > variables
>> > as scope so that they will get deleted when the rest of the object
>> > does?
>> > This could save coding of custom destructors in some cases.
>>
>> Custom destructors should NEVER destroy other GC allocated objects. the
>> GC
>> cannot guarantee the order of collection, meaning if you have a pointer
>> to
>> another GC allocated object, that object may have already been collected.
>>
>> There is no real reason to delete other objects in a destructor. If the
>> objects are no longer referenced, the GC will destroy those also. The
>> only
>> things you should destroy in a destructor are non-GC allocated resources,
>> such as file descriptors, or memory allocated with C's malloc.
>>
>> So to answer your question: scope is not necessary as a modifier for a
>> member variable.
>
> I wasn't asking about necessary. I was asking if it's valid. It's easy
> enough to construct RAII-like examples where a timely destruction of a
> member variable would be handy. It can also be handy when standard GC
> operation is overridden.
Not neccessary == not implemented :) Why implement a feature that is not
needed? I guess I should have stated that.
>From the attributes page (http://www.digitalmars.com/d/attribute.html):
"scope cannot be applied to globals, statics, data members, ref or out
parameters."
Timely destruction of a member that is not referenced in any other
object/thread should occur when the object that owns the member is
destroyed. No need to force it, which is why scope isn't necessary or
valid.
-Steve
More information about the Digitalmars-d
mailing list