GC collecting "too much"..

simendsjo simendsjo at gmail.com
Mon Mar 26 01:13:35 PDT 2012


On Sun, 25 Mar 2012 22:18:02 +0200, bearophile <bearophileHUGS at lycos.com>  
wrote:

> On Sunday, 25 March 2012 at 19:15:05 UTC, simendsjo wrote:
>> I'm doing some coding against a c library, and Ds GC keeps collecting c  
>> owned objects (I think - disabling the GC makes everything work)
>
> Three alternative solutions:
> - Allocate from the C heap the memory that C will need to use, and free  
> it manually or in a struct destructor (RAII) or with scope(exit).
> - Keep a pointer to the D-GC memory in the D code too.
> - In core.memory there are ways to disable scanning of a memory zone.  
> Maybe it's usable for your purposes too.
>
> Bye,
> bearophile


I've been able to find where the code fails, but now I don't understand  
what's happening at all.
Is calling GC.collect() from an extern(C) function undefined?

The following code just starts a mongoose web server and tries to run  
GC.collect in the handler.

extern(C) void* cb(mg_event event, mg_connection* conn, mg_request_info*  
request_info)
{
     GC.collect(); // segfault
     return null;
}

void main()
{
     auto opts = ["listening_ports", "6969"].map!(toUTFz!(char*))().array();
     mg_start(&cb, null, cast(const(char**))opts);
     GC.collect(); // no problem
     readln();
}

If I collect memory from main(), it works as expected (removing the  
collect from cb of course)
while(readln().chomp() != "q") { GC.collect(); }

The documentation in core.memory is a bit sparse on how the GC works. Are  
there any articles on the D GC?


More information about the Digitalmars-d-learn mailing list