GC interaction with malloc/free

DLearner bmqazwsx123 at gmail.com
Thu Jan 5 20:18:42 UTC 2023


On Thursday, 5 January 2023 at 19:54:01 UTC, H. S. Teoh wrote:
> On Thu, Jan 05, 2023 at 07:49:38PM +0000, DLearner via 
> Digitalmars-d-learn wrote:
>> Suppose there is a D main program (not marked anywhere with 
>> @nogc),
>> that _both_
>> 
>> A: Calls one or more C functions that themselves call 
>> malloc/free; and
>> also
>> B: Calls one or more D functions that themselves call 
>> malloc/free via
>> `import core.stdc.stdlib;`
>> 
>> Assuming the malloc/free's are used correctly, does this 
>> situation risk crashing the D main program?
> [...]
>
> core.stdc.stdlib.{malloc,free} *is* the exact same malloc/free 
> that C uses, it has nothing to do with the GC.  The allocated 
> memory is taken from the malloc/free part of the heap, which is 
> disjoint from the heap memory managed by the GC.
>
> So, it should not cause any crashes.
>
>
> T

That's comforting, but there is a reference in:

https://dlang.org/blog/2017/09/25/go-your-own-way-part-two-the-heap/

'...Given that it’s rarely recommended to disable the GC 
entirely, most D programs allocating outside the GC heap will 
likely also be using memory from the GC heap in the same program. 
In order for the GC to properly do its job, it needs to be 
informed of any non-GC memory that contains, or may potentially 
contain, references to memory from the GC heap.'

Followed by things that have to be done (GC.addRange) to avoid 
interaction effects?


More information about the Digitalmars-d-learn mailing list