Bad file descriptor in File destructor

Moritz Maxeiner via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 13 01:53:24 PDT 2017


On Thursday, 13 July 2017 at 08:38:52 UTC, unDEFER wrote:
> Hello! I have the code like this:
>
>     File file;
>     try {
>         file = File(path);
>     }
>     catch (Exception exp)
>     {
>         return;
>     }
>
> ...
>     try {
>
>     }

Where does that `File` come from? If it's std.stdio.File, that 
one is a struct with internal reference counting, so it shouldn't 
crash in the above. Could you provide a minimal working (in this 
case crashing) example?
If the `File` above is not std.stdio.File, but some custom type:
Be aware that structs have deterministic lifetimes, so `file`'s 
destructor will be called even when you return in the catch 
clause (on the default constructed `file`), so `File`'s 
destructor must check the field carrying the file descriptor for 
being valid; I advise setting such fields to be default 
constructed to some invalid value (e.g. `-1` in case of file 
descriptors).


More information about the Digitalmars-d-learn mailing list