+2 votes

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 Engine by (140 points)

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 :p

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories