]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
Avoid leaking memory while iterating hash tables
authorTravis Cross <tc@traviscross.com>
Sat, 30 Aug 2014 03:41:58 +0000 (03:41 +0000)
committerTravis Cross <tc@traviscross.com>
Sat, 30 Aug 2014 03:49:51 +0000 (03:49 +0000)
`switch_core_hash_first` allocates an iterator on each call that is
never freed except when the hash table is empty.

By using `switch_core_hash_first_iter` we allocate only one iterator,
and that iterator is freed after the last item is processed.

src/mod/applications/mod_directory/mod_directory.c
src/mod/applications/mod_redis/mod_redis.c
src/mod/endpoints/mod_rtmp/mod_rtmp.c
src/mod/event_handlers/mod_rayo/mod_rayo.c

index 9baaf8bcf12f6f480a9b38c015e530a20d1ddf7e..e37b89a66035071c9701d5dcc2c3a5098cda8e08 100644 (file)
@@ -1091,7 +1091,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_directory_load)
    Macro expands to: switch_status_t mod_directory_shutdown() */
 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_directory_shutdown)
 {
-       switch_hash_index_t *hi;
+       switch_hash_index_t *hi = NULL;
        dir_profile_t *profile;
        void *val = NULL;
        const void *key;
@@ -1100,7 +1100,7 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_directory_shutdown)
 
        switch_mutex_lock(globals.mutex);
 
-       while ((hi = switch_core_hash_first(globals.profile_hash))) {
+       while ((hi = switch_core_hash_first_iter(globals.profile_hash, hi))) {
                switch_core_hash_this(hi, &key, &keylen, &val);
                profile = (dir_profile_t *) val;
 
index 3c8fc3f79d96473789dddfc2e8d3d020a3fc3b8b..5e84b196e909945e375e89055d530f655bccc817 100644 (file)
@@ -152,7 +152,6 @@ SWITCH_LIMIT_RELEASE(limit_release_redis)
        switch_channel_t *channel = switch_core_session_get_channel(session);
        limit_redis_private_t *pvt = switch_channel_get_private(channel, "limit_redis");
        int val, uuid_val;
-       switch_hash_index_t *hi;
        char *rediskey = NULL;
        char *uuid_rediskey = NULL;
        int status = SWITCH_STATUS_SUCCESS;
@@ -171,8 +170,9 @@ SWITCH_LIMIT_RELEASE(limit_release_redis)
 
        /* clear for uuid */
        if (realm == NULL && resource == NULL) {
+               switch_hash_index_t *hi = NULL;
                /* Loop through the channel's hashtable which contains mapping to all the limit_redis_item_t referenced by that channel */
-               while ((hi = switch_core_hash_first(pvt->hash))) {
+               while ((hi = switch_core_hash_first_iter(pvt->hash, hi))) {
                        void *p_val = NULL;
                        const void *p_key;
                        char *p_uuid_key = NULL;
index 6a7e4a45b7d49f849e574b7842e21557a9f4feb9..928ec58fe9268a8780e7c976101c26a2680d47eb 100644 (file)
@@ -1078,7 +1078,7 @@ fail:
 
 switch_status_t rtmp_profile_destroy(rtmp_profile_t **profile) {
        int sanity = 0;
-       switch_hash_index_t *hi;
+       switch_hash_index_t *hi = NULL;
        switch_xml_config_item_t *instructions = get_instructions(*profile);
        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Stopping profile: %s\n", (*profile)->name);
        
@@ -1087,7 +1087,7 @@ switch_status_t rtmp_profile_destroy(rtmp_profile_t **profile) {
        switch_thread_rwlock_wrlock((*profile)->rwlock);
        
        /* Kill all sessions */ 
-       while ((hi = switch_core_hash_first((*profile)->session_hash))) {
+       while ((hi = switch_core_hash_first_iter((*profile)->session_hash, hi))) {
                void *val;
                rtmp_session_t *session;
                const void *key;
index bf43a1f21a9eca062f7b17de20c387f751abbc87..00e74f8e47589d35b1c48382c3ca7281e9b1d555 100644 (file)
@@ -1553,7 +1553,7 @@ void rayo_peer_server_send(struct rayo_actor *server, struct rayo_message *msg)
  */
 static void rayo_peer_server_cleanup(struct rayo_actor *actor)
 {
-       switch_hash_index_t *hi;
+       switch_hash_index_t *hi = NULL;
        struct rayo_peer_server *rserver = RAYO_PEER_SERVER(actor);
 
        /* a little messy... client will remove itself from the peer server when it is destroyed,
@@ -1561,7 +1561,7 @@ static void rayo_peer_server_cleanup(struct rayo_actor *actor)
         * the server must remove the client.
         */
        switch_mutex_lock(globals.clients_mutex);
-       while ((hi = switch_core_hash_first(rserver->clients))) {
+       while ((hi = switch_core_hash_first_iter(rserver->clients, hi))) {
                const void *key;
                void *client;
                switch_core_hash_this(hi, &key, NULL, &client);