Question about g_object_unref() memory leak

g_object_unref() exist memory leak

software version: kernel:4.19 polkit:0.115

test code:

#include <polkit/polkit.h>

int main(int argc, char *argv[])
{
  int i;
  while (1)
  {
    PolkitAuthority *pa = NULL;
    pa = polkit_authority_get_sync(NULL, NULL);
    g_object_unref(pa);
    sleep(1);
  }
  return 0;
}

There have 4GB memory leak when execute this program in three hours.

The original issue : IsClientAuthorized() memory leak · Issue #87 · LudovicRousseau/PCSC · GitHub

Nobody is going to believe there is a memory leak in g_object_unref(), because it doesn’t allocate any memory…

Sure looks like the allocation is happening in polkit. Also, shouldn’t you be running a GMainLoop or at least iterating the GMainContext?

Seems like OP agrees…

Given DBus is involved it wouldn’t surprise me if this goes away by g_main_context_iteration’ing

1 Like

Oddly, if the function “g_initable_init” is commented out, there will be no leakage. The memory requested by the corresponding polkit_authority_initable_init in the polkit code is also released in the finalize function invoked during unref.

post more info, remove the “sleep” statement from the test case, and run it about 18 hours, the memory can be increased to 17GB.
image

Right, sleep() won’t iterate the main context, just block.

To be clear, what is likely happening is that work is being done off the main thread, but because the program never releases control to a running GMainLoop or iterates the GMainContext manually those tasks are never allowed resolve.

There is an example in the repository linked above showing use of a GMainLoop to iterate the GMainContext.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.