Memory leak issue between extern (c) and D function

Guillaume Piolat first.last at spam.org
Fri Apr 14 11:15:59 UTC 2023


On Friday, 14 April 2023 at 04:43:39 UTC, Paul Backus wrote:
>
> If you want the GC to clean up your memory, use `new` to 
> allocate it instead of `malloc`. Like this:
>
> ```d
> mystruct* getmystruct()
> {
>     return new mystruct;
> }
> ```

That won't work because the C++ programm calling the D dynlib 
will not have its stack scanned, leading to that object being 
reclaimed early.

OP could add another extern(C) D function to free the allocated 
object.
Or another extern(C) D function to call GC.addRoot


More information about the Digitalmars-d-learn mailing list