Something wrong with GC

thedeemon via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Mar 21 00:55:39 PDT 2016


On Sunday, 20 March 2016 at 07:49:17 UTC, stunaep wrote:
> The gc throws invalid memory errors if I use Arrays from 
> std.container.

Those arrays are for RAII-style deterministic memory release, 
they shouldn't be freely mixed with GC-allocated things. What 
happens here is while initializing Array sees it got some GC-ed 
value type (strings), so it tells GC to look after those strings. 
When your program ends runtime does a GC cycle, finds your Test 
object, calls its destructor that calls Array destructor that 
tries to tell GC not to look at its data anymore. But during a GC 
cycle it's currently illegal to call such GC methods, so it 
throws an error.
Moral of this story: try not to store "managed" (collected by GC) 
types in Array and/or try not to have Arrays inside "managed" 
objects. If Test was a struct instead of a class, it would work 
fine.


More information about the Digitalmars-d-learn mailing list