InputRange help: (1) repeated dtor calls and (2) managing resources needing free()

James Blachly james.blachly at gmail.com
Mon Aug 13 04:23:49 UTC 2018


On Thursday, 14 June 2018 at 00:42:25 UTC, James Blachly wrote:
> ...
> I assume the (apparent) lack of parity between ctor and dtor is 
> because the "default postblit" (which I figured out for a 
> struct means an empty `this(this)` ctor) is called when a copy 
> is made. My understanding is that I cannot disable the default 
> postblit and still act as a range, correct? Should I be 
> overloading this?
>
> 2. Directly related to the above, I need, when the range is 
> consumed, to free() the underlying library's iterator handle. 
> Naively, I had the destructor do this, but obviously with 
> multiple calls to ~this I end up with an error free()'ing a 
> pointer that is no longer alloc'd.  What is the correct way to 
> handle this situation in D?
>
> Other Range and destructor advice generally (e.g., "You should 
> totally change your design or approach to X instead") is always 
> welcomed.
>
> James

I think I have a handle on #1 (copy of the range is made for 
consumption which is why dtor is called more often than ctor), 
but would still be interested in advice regarding #2 (as well as 
general Range and dtor advice).

Here: 
https://github.com/blachlylab/dhtslib/blob/master/source/dhtslib/tabix.d#L98 I need to free the library's iterator, but the Range's destructor is the wrong place to do this, otherwise memory is freed more than once.

Is it a better approach to (a) somehow guard the call to 
tbx_itr_destroy or (b) create a postblit that creates a new 
iterator and pointer? (or (c), None of the above) As above, my 
understanding is that disabling the default posblit prohibits 
acting as a Range.

Thanks in advance


More information about the Digitalmars-d-learn mailing list