Reputation: 1
I have a memory leak in my program and cannot understand why.
My program is multi-threaded and uses json-c and glib.
Thread 1 calls the following method every time it receives data over a UDP stream.
void foo(state_t *self, const char *data)
{
char *x = strdup(data);
g_async_queue_push(self->write_queue, x);
}
where write_queue
is a GAsyncQueue
initialized with g_async_queue_new()
.
Thread 2 processes the data in an endless loop.
static void bar (state_t *self)
{
while (1)
{
void *msg = g_async_queue_pop(self->write_queue);
parse_json ((char*)msg);
free(msg);
}
}
where
void parse_json (const char *data)
{
struct json_object *elements;
elements = json_tokener_parse(data);
json_object_put(elements);
}
Threads are created with the g_thread_new()
command.
When I run this program, I see memory consumption grow constantly and regularly with htop (roughly 100MB/sec).
When I comment out the call to parse_json()
, the memory leak disappears.
When I run parse_json()
in a unit test with a large for loop, I notice no memory leak.
What I am missing here?
NB: I notice that I get roughly the same volume of memory leak if I comment out both the call to parse_json()
and the free(msg)
. This leads me to think that my code is keeping a reference count somewhere on the msg
string.
I am using this version of Linux
Linux exp-infra 5.11.0-49-generic #55-Ubuntu SMP Wed Jan 12 17:36:34 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
I am compiling my code with g++ --std=c++20 -O2
.
My version of g++ is 10.3.0.
Thanks!
Upvotes: 0
Views: 306