Memory leak issue between extern (c) and D function

backtrack me at alchemy.in
Fri Apr 14 17:31:02 UTC 2023


On Friday, 14 April 2023 at 11:15:59 UTC, Guillaume Piolat wrote:
> 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

Thank you for your response.

I added something like this in extern (c) function.

```
        __delete(output);
```
When i debugged, it shows null to output after executing above 
line. however the memory is not releasing.

Thank you.



More information about the Digitalmars-d-learn mailing list