From 9927956104340dd459daebec49181936518972fa Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Sat, 9 Aug 2014 23:20:21 +0200 Subject: kike: fix leaking of channels --- src/kike.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/kike.c b/src/kike.c index 18a906f..cc63195 100644 --- a/src/kike.c +++ b/src/kike.c @@ -370,10 +370,10 @@ struct channel struct str_vector invite_list; ///< Exceptions from +I }; -static void -channel_init (struct channel *self) +static struct channel * +channel_new (void) { - memset (self, 0, sizeof *self); + struct channel *self = xcalloc (1, sizeof *self); self->user_limit = -1; self->topic = xstrdup (""); @@ -381,10 +381,11 @@ channel_init (struct channel *self) str_vector_init (&self->ban_list); str_vector_init (&self->exception_list); str_vector_init (&self->invite_list); + return self; } static void -channel_free (struct channel *self) +channel_delete (struct channel *self) { free (self->name); free (self->key); @@ -400,6 +401,8 @@ channel_free (struct channel *self) str_vector_free (&self->ban_list); str_vector_free (&self->exception_list); str_vector_free (&self->invite_list); + + free (self); } static char * @@ -466,9 +469,9 @@ server_context_init (struct server_context *self) str_map_init (&self->users); self->users.key_xfrm = irc_strxfrm; - // TODO: set channel_free() as the free function? str_map_init (&self->channels); self->channels.key_xfrm = irc_strxfrm; + self->channels.free = (void (*) (void *)) channel_delete; str_map_init (&self->handlers); self->handlers.key_xfrm = irc_strxfrm; @@ -587,12 +590,10 @@ channel_user_count (const struct channel *chan) static struct channel * channel_create (struct server_context *ctx, const char *name) { - struct channel *chan = xcalloc (1, sizeof *chan); - channel_init (chan); - str_map_set (&ctx->channels, name, chan); - + struct channel *chan = channel_new (); chan->ctx = ctx; chan->name = xstrdup (name); + str_map_set (&ctx->channels, name, chan); return chan; } @@ -600,11 +601,7 @@ static void channel_destroy_if_empty (struct server_context *ctx, struct channel *chan) { if (!chan->users) - { str_map_set (&ctx->channels, chan->name, NULL); - channel_free (chan); - free (chan); - } } // --- Clients ----------------------------------------------------------------- -- cgit v1.2.3-70-g09d2