aiekick/ImGuiFileDialog

Mismatched free() / delete / delete [] when using IGFD_GetFilePathName

AdrianoDiDio opened this issue ยท 6 comments

Describe the bug
Hello, I'm using this library trough the C binding and I found an issue with memory allocations.
The issue came up after I ran valgrind in my program and it reported 'Mismatched free() / delete / delete []' when calling the function
IGFD_GetFilePathName().
After checking the source code I found that the function allocates memory using new[] and in the documentation it is stated that to free
that string,after you obtained the path, you need to call free.
In my understanding this is not allowed in C++ and can cause potential issues.
This problem is present in all the IGFD_Get* functions.
A simple solution would be to replace new with malloc, so that it can be freed from the C code.

Hello,

indeed make sense.

not sure if the best way is to use malloc,
maybe just change the behavior of these functions by passing as args instead of return type ?
like that the used char buffer will be yours

tried your proposal btw, but cant test it by now.

can you test that in the branch : Lib_Only_C_Memory_leaks

Thanks for the quick response, yes it is working as expected and it no longer reports any problems.
One potential solution, as you said, would be to change the return signature to int (in order to indicate any problem) and add an additional argument to store the path.
I've also noticed you updated the function IGFD_GetSelection to use malloc, this is not necessary since there is a corresponding function (IGFD_Selection_DestroyContent) that is able to call the destructor from C++ side and can be left as it is.

thanks for the quick test :)

ok yes i will do that for the args, and yes you are right regarding IGFD_GetSelection

will be done the next week, im not at my home this weekend

finally i not changed the args logic, but applied our fix.

can you check if ok ?

thanks

Yes, It's working as expected and no longer reporting any error.
I didn't test all the functions but based on the changes applied to the code It should work without any problems.

ok thanks for your support :)