[gnet-dev] Possible Memory Leak



Hi,

I think there is a memory leak somewhere.  I made the following
observations with the program below.

o When I comment out the gnet_server_new line, the memory consumption
drops by 8.
o When I comment out the gnet_conn_set_watch_error line, the memory
stays constant, no matter how many clients connect.
o With the code exactly as below, the memory increases by 8 for each
_concurrent_ client (see the maximum variable).
o When a gnet conn client is connected and properly destroyed, it needs
8 bytes more than when it fails to connect (program not shown).

So is there a leak in my code, or in gnet (unlikely, looking at its nice
cleanup code) or in glib (probably somewhere in the watch/source stuff?

Carsten


#include <gnet.h>


static int          clients = 0, maximum = 0;


static gboolean
timeout_quit (gpointer data)
{
  GMainLoop        *loop = data;

  if (clients == 0)
    {
      g_main_loop_quit (loop);
      return FALSE;
    }
  else
    {
      return TRUE;
    }
}

static gboolean
conn_delete (gpointer data)
{
  GConn            *conn = data;

  --clients;
  g_message ("Delete connection, now %d", clients);
  gnet_conn_delete (conn);
  return FALSE;
}

static void
server_accept (GServer *s, GConn *conn, gpointer data)
{
  if (!conn)
    {
      g_error ("Server error");
    }

  ++clients;
  if (clients > maximum)
    {
      maximum = clients;
    }
  g_message ("Accept connection, now %d", clients);
  gnet_conn_set_watch_error (conn, TRUE);
  g_timeout_add (1000, conn_delete, conn);
}

int
main (int argc, char **argv)
{
  GMainLoop        *loop = NULL;
  GServer          *s = NULL;
  
  g_mem_set_vtable (glib_mem_profiler_table);

  loop = g_main_loop_new (NULL, FALSE);
  g_timeout_add (3000, timeout_quit, loop);

  s = gnet_server_new (NULL, 33333, server_accept, NULL);
  if (!s)
    {
      g_error ("Server create");
    }
  
  g_main_loop_run (loop);
  g_assert (clients == 0);
  g_message ("Maximum of %d concurrent client(s)", maximum);

  g_main_loop_unref (loop);
  if (s)
    {
      gnet_server_delete (s);
    }
  
  g_mem_profile ();
  return 0;
}





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]