Segmentation error at the end problem (148 line program listing)
Gide Nwawudu
gide at btinternet.com
Thu Jan 29 03:35:41 PST 2009
On Wed, 28 Jan 2009 13:37:40 -0800, Charles Hixson
<charleshixsn at earthlink.net> wrote:
>Gide Nwawudu wrote:
>> On Tue, 27 Jan 2009 22:48:33 -0800, Charles Hixson
>> <charleshixsn at earthlink.net> 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?
>>
>> I'm thinking it might be an issue with close and the dtor being called
>> on the same object. If you add std.gc.fullCollect() after the bf =
>> null. Does that make the code seg fault before 'after close' is
>> written?
>>
>> Gide
>I had to wriggle the code around a bit. (It's D2 not D1.) However it
>didn't make any difference to do:
>void main()
>{
> try
> { BlockFile bf;
> bf = new BlockFile ("test.bf", 4096);
> writefln ("before close");
> bf.close;
> bf = null;
> GC.collect;
> writefln ("after close");
> BlockFile cf = new BlockFile ("test.bf", 4096);
> writefln ("after second open");
> }
> catch (Exception e)
> { writefln ("Caught Exception ", e); }
>}
>
> -------------------
>And the docs say that GC.collect does a full collect;
>
>P.S.: Attached is the full listing
Calling close and dtor on BufferedFile is the problem. Rewiting
BlockFile.close as follows fixes the problem, but I think your code
should work.
void close() { delete bf; bf = null; }
Gide
More information about the Digitalmars-d-learn
mailing list