Why would an initialised struct pointer field be null in the struct's destructor?

Stanislav Blinov via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 21 18:02:40 PDT 2017


On Monday, 22 May 2017 at 00:45:27 UTC, Adam D. Ruppe wrote:
> On Monday, 22 May 2017 at 00:36:24 UTC, Stanislav Blinov wrote:
>> I can't think of any case where you'd want preconditions on 
>> destructor when the object is in .init state.
>
> I think we're actually saying the same thing: I mean the 
> destructor must be callable on .init so you might do like
>
> struct Foo {
>    int* ptr;
>    ~this() {
>       // might be called with ptr is null cuz of .init
>       // so wanna check
>       if(ptr !is null) free(ptr);
>     }
> }

Ah, yes, exactly. The page is indeed the same one.

P.S. Though it's fine to call free with a null pointer :P


More information about the Digitalmars-d-learn mailing list