GDNative - What needs to be freed from memory?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Beamer159

I am having trouble determining what variables need to be freed in GDNative. I have a function like this"

void function(godot_variant var) {
    godot_string gs_var = api->godot_variant_as_string(&var);
    godot_char_string gcs_var = api->godot_string_utf8(&gs_var);
    const char *c_var = api->godot_char_string_get_data(&gcs_var);
}

Do these functions, godot_variant_as_string, godot_string_utf8, and godot_char_string_get_data dynamically allocate memory? I don’t think godot_char_string_get_data dynamically allocates memory because its return type is const char *, and the GLFWDemo on the GitHub site implies that godot_string_utf8 does dynamically allocate memory, whereas godot_variant_as_string does not. However, that seems like a bizzare inconsistency to me. What about other GDNative functions, such as godot_string_to_upper or godot_string_is_numeric? Do all of these functions return dynamically allocated memory, do none of them, or is it some weird mix?

In doubt, here is the implementations:

as_string()
https://github.com/godotengine/godot/blob/master/modules/gdnative/gdnative/variant.cpp#L252

utf8()
https://github.com/godotengine/godot/blob/master/modules/gdnative/gdnative/string.cpp#L950

get_data doesn’t allocate because it basically gives access to the internal buffer rather than copying it.

For the others, if a function for freeing them exists, I guess they should be used because they call the destructor. A destructor not called means potential leak :stuck_out_tongue:

Zylann | 2018-11-30 18:53