Example contains wrong code
// Retrieve all the visible text between the two bounds
g_autofree char *text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
// If there is nothing to save, return early
if (text == NULL || *text == '\0')
return;
// Start the asynchronous operation to save the data into the file
g_file_replace_contents_async (file
text,
strlen (text),
NULL,
FALSE,
G_FILE_CREATE_NONE,
NULL,
save_file_complete,
self);
Using g_autofree
effectively frees the text at the end of the function but the application must keep it available until after calling g_file_replace_contents_finish()
.
If I was writing the code, I'd probably replace it with something like:
// Retrieve all the visible text between the two bounds
char *text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
// If there is nothing to save, return early
if (text == NULL || *text == '\0')
{
g_free (text);
return;
}
g_autoptr(GBytes) bytes = g_bytes_new_take (text, strlen (text));
// Start the asynchronous operation to save the data into the file
g_file_replace_contents_bytes_async (file
bytes,
NULL,
FALSE,
G_FILE_CREATE_NONE,
NULL,
save_file_complete,
self);
Edited by Benjamin Otte