Memory leak issue between extern (c) and D function

backtrack me at alchemy.in
Fri Apr 14 03:50:37 UTC 2023


Dear All, I am new to D lang. I have been given a task to consume 
the .dll generated from a D lang project.

I added extern (c) function for call the .dll from CPP file. i 
have code like below


```
// myfile.d
extern(c)
{

     mystruct* getmystruct()
     {
         mystruct* mystruct =  
cast(mystruct*)malloc(mystruct.sizeof);
         return mystruct;

     }

    char* do_some_work(const mystruct* mystruct , immutable(char)* 
  input, int inputlenght)
    {

        InputStruct input;
        input.name = input[0..inputlenght];
        auto output = mystruct.getValue(input);
        // do some process and return char*
        return output_char.ptr;
     }

}


struct mystruct
{
    OutputStruct[] getValue(InputStruct input)
   {
     // some string operation and other operation done here
     return output
   }
}

struct InputStruct
{
    string name;
}

struct OutputStruct
{
    string name;
    string value;
}

```

Now i have my cpp file which calls like below

```
mystruct* mystruct = lib.getmystruct();
char* output = lib.do_some_work(mystruct, "input", 5);

```

The problem i am facing is, the memory keep on increasing and i 
am not able to fix the memory issue. I am suspecting that since 
the D lang function is called from extern(c) function GC is not 
clearing the memeory.

Can you please help me on this?

Thank you


More information about the Digitalmars-d-learn mailing list