Unittest hangs on completion

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 1 11:09:56 PST 2017


On 12/30/16 9:32 PM, David Zhang wrote:
> On Saturday, 31 December 2016 at 02:03:07 UTC, rikki cattermole wrote:
>> As it should, current is never reassigned.
>> You only need one var, next. Of course I didn't read the entire thread
>> chain so, I'm probably missing something.
>>
>> import std.experimental.allocator;
>>
>> void main() {
>>         struct S { S* next; }
>>         S* _foo;
>>         foreach (e; 0 .. 10)
>>                 _foo = theAllocator.make!S(_foo);
>>         S* next;
>>
>>         next = _foo;
>>         while(next !is null) {
>>                 auto nextT = next.next;
>>                 theAllocator.dispose(next);
>>                 next = nextT;
>>         }
>> }
>
> Thanks for your response. So next is never null, and thus there is an
> infinite loop, correct?

This is correct. Rewriting the code as rikki has would be correct code.

However, I would also caution about your original code that deallocates 
inside a destructor. You cannot access nor try to free reference members 
of a GC-allocated class inside the destructor. It's explicitly cautioned 
against here: http://dlang.org/spec/class.html#destructors

"Furthermore, the order in which the garbage collector calls destructors 
for unreference objects is not specified. This means that when the 
garbage collector calls a destructor for an object of a class that has 
members that are references to garbage collected objects, those 
references may no longer be valid. This means that destructors cannot 
reference sub objects."

> If so, why does dub indicate that all tests are
> complete?

Because dub is running the tests and then running your main. Unit tests 
run before main does.

-Steve


More information about the Digitalmars-d-learn mailing list