Either I'm confused or the gc is

user1234 user1234 at 12.de
Thu Oct 22 23:41:41 UTC 2020


On Thursday, 22 October 2020 at 23:07:28 UTC, H. S. Teoh wrote:
> On Thu, Oct 22, 2020 at 10:05:42PM +0000, user1234 via 
> Digitalmars-d wrote: [...]
>> As you've been said in one of the first answer this must be 
>> because D GC sees a pointer allocated with malloc, and this 
>> pointer looks orphan to the GC as it has no root.
>
> The GC does not collect objects allocated by malloc. (That 
> would not make any sense, since if you allocated it with 
> malloc, obviously you intend to deallocate it with free!)
>
>
> T

Yeah you're right. I was actually thinking to the opposite case:
the parent is mallocated but the members are not, eg

---
import core.memory;
import std.experimental.allocator;
import std.experimental.allocator.mallocator;
import std.experimental.allocator.common;

enum fill = "azertyuiopqsdfghjklm";

struct Node
{
     this(string c){content = c;}
     string content;
     Node*[] nodes;
}

void main()
{
     Node* root = make!Node(Mallocator.instance, fill);
     foreach(immutable i; 0 .. 10000)
     {
         root.nodes ~= make!Node(Mallocator.instance, fill);
         foreach(immutable j; 0 .. 100)
             root.nodes[i].nodes ~= make!Node(Mallocator.instance, 
fill);
     }
     assert(root.content == fill);
     foreach(immutable i; 0 .. root.nodes.length)
     {
         assert(root.nodes[i].content == fill);
         foreach(immutable j; 0 .. root.nodes[i].nodes.length)
             assert(root.nodes[i].nodes[j].content == fill);
     }
}
---

which crashes because of that (it "should" crash but I dont have 
much memory installed so maybe this is not the case for everyone)


More information about the Digitalmars-d mailing list