January 31, 2009 Re: Segmentation error at the end problem (148 line program listing) | ||||
---|---|---|---|---|
| ||||
Posted in reply to grauzone |
grauzone wrote:
> Daniel Keep wrote:
>> [snip]
>>
>> 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.
Personally, I'd be happy if I could get this to work:
class Foo : Disposable
{
void dispose()
{
// do cleanup
}
~this(bool deterministic)
{
if( deterministic )
{
if( ! isDisposed )
dispose;
}
else if( ! isDisposed )
throw new DisposeException("You forgot to dispose, you thick
burke!");
}
}
-- Daniel
|
February 01, 2009 Solution Re: Segmentation error at the end problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charles Hixson | Charles Hixson wrote:
> Main routine:
>
> void main()
> {
> try
> { BlockFile bf;
> bf = new BlockFile ("test.bf", 4096);
> writefln ("before close");
> bf.close;
> bf = null;
> writefln ("after close");
> BlockFile cf = new BlockFile ("test.bf", 4096);
> writefln ("after second open");
> }
> catch (Exception e)
> { writefln ("Caught Exception ", e); }
> }
>
> Results in:
> Exiting BlockFile::this
> before close
> after close
> Exiting BlockFile::this
> after second open
> Segmentation fault
>
> I could post all the code. It's only 146 lines. But perhaps this is enough?
So after the discussion I decided that it appeared to make BlockFile into a scope class. I did, removing all destructors and delete operations. The new main method was:
void main()
{
{ scope BlockFile bf;
bf = new BlockFile ("test.bf", 4096);
writefln ("before close");
}
{
writefln ("after close");
scope BlockFile cf = new BlockFile ("test.bf", 4096);
writefln ("after second open");
writefln ("after writefln");
}
}
Yielding:
Exiting BlockFile::this
before close
after close
Exiting BlockFile::this
after second open
after writefln
Apparently this can't be handled with garbage collection.
|
Copyright © 1999-2021 by the D Language Foundation