Hi!
I’m using GLIB to read ini files. I’ve created helper function to use along all parts of my program (written in C).
This is the function to read integer:
int ini_getInteger(char *ini_file, char *section, char *key, int def_value) {
GKeyFile *localini;
GError *error = NULL;
int abc = 0;
localini = g_key_file_new();
if (g_key_file_load_from_file(localini, ini_file, G_KEY_FILE_KEEP_COMMENTS, &error) == FALSE) {
mylog_log("Error loading ini file.\n");
g_key_file_free(localini);
if (error != NULL) {
g_error_free(error);
}
return def_value;
}
abc = g_key_file_get_integer(localini, section, key, &error);
g_key_file_free(localini);
if (error != NULL) {
if (error->code == G_KEY_FILE_ERROR_KEY_NOT_FOUND || error->code == G_KEY_FILE_ERROR_INVALID_VALUE || error->code == G_KEY_FILE_ERROR_GROUP_NOT_FOUND) {
g_error_free(error);
return def_value;
} else {
g_error_free(error);
return abc;
}
} else {
return abc;
}
}
And this is an example of use (from monitor.c):
if (ini_getInteger(config_file, "server", "debug_level", 0) != debug_level) {
debug_level = ini_getInteger(config_file, "server", "debug_level", 0);
}
The function is working, I’m getting the correct value. The problem is that I’m getting memory leak too.
This is the output from valgrind:
==2505==
==2505== HEAP SUMMARY:
==2505== in use at exit: 108,497 bytes in 785 blocks
==2505== total heap usage: 13,086 allocs, 12,301 frees, 622,799 bytes allocated
==2505==
==2505== 184 (88 direct, 96 indirect) bytes in 1 blocks are definitely lost in loss record 52 of 61
==2505== at 0x6CA177F: malloc (vg_replace_malloc.c:299)
==2505== by 0x72EB8D0: g_malloc (in /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0.5800.3)
==2505== by 0x7303832: g_slice_alloc (in /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0.5800.3)
==2505== by 0x72D3D2D: g_hash_table_new_full (in /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0.5800.3)
==2505== by 0x72BFCF0: g_get_language_names_with_category (in /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0.5800.3)
==2505== by 0x72DB58B: ??? (in /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0.5800.3)
==2505== by 0x72DD22C: g_key_file_new (in /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0.5800.3)
==2505== by 0x11AB56: ini_getInteger (utils.c:459)
==2505== by 0x10FFCD: thread_monitor (monitor.c:138)
==2505== by 0x6CC7FA2: start_thread (pthread_create.c:486)
==2505== by 0x75054CE: clone (clone.S:95)
==2505==
==2505== LEAK SUMMARY:
==2505== definitely lost: 88 bytes in 1 blocks
==2505== indirectly lost: 96 bytes in 2 blocks
==2505== possibly lost: 0 bytes in 0 blocks
==2505== still reachable: 108,313 bytes in 782 blocks
==2505== suppressed: 0 bytes in 0 blocks
==2505== Reachable blocks (those to which a pointer was found) are not shown.
==2505== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==2505==
==2505== For counts of detected and suppressed errors, rerun with: -v
==2505== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
What am I doing wrong?
GLIBC Version: 2.28