Segmentation error at the end problem (148 line program listing)

grauzone none at example.net
Sat Jan 31 07:56:22 PST 2009


Daniel Keep wrote:
> 
> grauzone wrote:
>> The garbage collector isn't guaranteed to to free and destroy an
>> unreachable object. That's because the GC is conservative. So if you
>> want to be sure the object's resources are freed, you have to do it
>> explicitly.
>>
>> I think you have two choices:
>> 1. Remove close() from the destructor, and call close() manually when
>> you're done.
>> 2. Use scope or delete to ensure the destructor is always directly
>> called, and never by the GC.
>>
>>
>> Here's how you can use scope:
>>
>> {
>>     scope BlockFile f = new BlockFile(...);
>>     //... do something with f
>> } //f goes out of scope, and the compiler inserts delete f;
> 
> If you're going to do that, you really should make the it a scope class
> to ensure you never accidentally let the GC try to delete it.
> 
> I (and a few others) petitioned what feels like years ago for a simple
> argument to dtors to distinguish between deterministic destruction
> (delete/scope) and automatic destruction (GC).  Never gained any ground,
> sadly.

I think it'd be even better to make them different functions. The 
finalizer could just be a virtual function called finalize(). Really, 
the differences between proper destructors and finalizers should be 
large enough to justify separate functions.

Or even better, ditch finalizers and their wacky semantics (which make 
them quite useless anyway), and invent something like notify-able 
weakpointers. D already provides basic support for this 
(Object.notifyRegister()), but I think in Phobos 1.0 it's a bit buggy. 
There were some issues with race conditions and locking.


More information about the Digitalmars-d-learn mailing list