How to free a string returned to expert from external dll

 

If my expert is calling a function on an external dll, and this function returns a string (char *) allocated on the dll

How can i free the string resource after the expert is done with it

 

i assume your string looks like this

string the_name_of_string = "your_import_text";

if so then

string the_name_of_string = "";
 
qjol:

i assume your string looks like this

if so then


no this doesn't work

Even if i return an empty string from the dll, the memory is not being dealocated, it just keeps growing up, i think that the solution may be to send a reference of a mql string to the dll, and assign it on the dll side

 

as far as i know MT4 will copy the string from the returned pointer to its own memory. you should be able to deallocate it in your dll *after* the function has returned or maintain your own pool of memory and just mark it as unused again.

If you would use something more advanced than C++ like for example Object Pascal then you would have memory managed and reference counted strings that would be allocated and deallocated automatically when the reference count drops to zero when the function exits. You could then just return a pointer to the string and don't care about it anymore after your function has exited, the heap manager in your DLL would care about it afterwards while MT4 would have copied it to its own memory already.

 

an alternative would be to allocate the string in mql4 (by creating a dummy string of the needed length) and pass a pointer to it to the dll (pass the string by reference and your dll will receive a pointer and can write to this memory).

 

Thanks guys' - as i know the format and length of the string the most simple solution for me was to allocate the string on the mt4 side, and pass a reference to it to the dll, then copy the result on the dll side .

it works and without any memory leaks.

Reason: