Sane way to deallocate strings from DLL

 

My DLL specifically constructs strings for my MQL5 code (eg UTF16, null terminated, on the heap).
As far as I understood, metatrader will then automatically copy this string for the EA to use.

The problem now is that the specifically constructed string still lies somewhere in memory, causing a memory leak.
I can think of a few ways to free those strings again, but they all make the API of the DLL significantly more shitty. Is there any pattern to tackle this in a sane way?

 
ggmoritz:

My DLL specifically constructs strings for my MQL5 code (eg UTF16, null terminated, on the heap).
As far as I understood, metatrader will then automatically copy this string for the EA to use.

The problem now is that the specifically constructed string still lies somewhere in memory, causing a memory leak.
I can think of a few ways to free those strings again, but they all make the API of the DLL significantly more shitty. Is there any pattern to tackle this in a sane way?

Well, you probably need to test this, but, let's say you construct an object on the heap in MQL with a uchar member as array, and an int with the corresponding size of the array.

Now you should be able to send this object by reference to your DLL, and be able to allocate memory for the uchar array, save the size in the object.

Now you should be able to deallocate this array in MQL again. And you can transform the uchar to a string with CharArrayToString.

As long as the uchar array passed inside the object to the DLL is of size zero, this will leave you with no memory leaks.
Reason: