diff src/dnsbl.cpp @ 146:7278c9766e26

free old configs when last reference goes away
author carl
date Sun, 15 Oct 2006 17:21:07 -0700
parents ecb40aa3eaa5
children 9330b8d6a56b
line wrap: on
line diff
--- a/src/dnsbl.cpp	Sun Oct 15 12:41:46 2006 -0700
+++ b/src/dnsbl.cpp	Sun Oct 15 17:21:07 2006 -0700
@@ -261,7 +261,9 @@
 	return_fd();
 	pthread_mutex_lock(&config_mutex);
 		pc->reference_count--;
+		bool last = (!pc->reference_count) && (pc != config);
 	pthread_mutex_unlock(&config_mutex);
+	if (last) delete pc;  // free this config, since we were the last reference to it
 	reset(true);
 }
 
@@ -1160,15 +1162,13 @@
 
 ////////////////////////////////////////////////
 //	thread to watch the old config files for changes
-//	and reload when needed. we also cleanup old
-//	configs whose reference count has gone to zero.
+//	and reload when needed.
 //	we also clear the SMTP AUTH recipient counts hourly
 //
 void* config_loader(void *arg);
 void* config_loader(void *arg) {
 	int loop = 0;
 	typedef set<CONFIG *> configp_set;
-	configp_set old_configs;
 	while (loader_run) {
 		sleep(180);  // look for modifications every 3 minutes
 		if (!loader_run) break;
@@ -1198,10 +1198,11 @@
 			if (newc) {
 				// replace the global config pointer
 				pthread_mutex_lock(&config_mutex);
-					CONFIG *old = config;
+					CONFIG *pc = config;
+					bool last = pc && (!pc->reference_count);
 					config = newc;
 				pthread_mutex_unlock(&config_mutex);
-				if (old) old_configs.insert(old);
+				if (last) delete pc;  // there were no references to this config
 			}
 			else {
 				// failed to load new config
@@ -1211,20 +1212,6 @@
 				dc.load_time = time(NULL);
 			}
 		}
-		// now look for old configs with zero ref counts
-		for (configp_set::iterator i=old_configs.begin(); i!=old_configs.end(); ) {
-			CONFIG *old = *i;
-			if (!old->reference_count) {
-				if (debug_syslog) {
-					char buf[maxlen];
-					snprintf(buf, sizeof(buf), "freeing memory for old configuration generation %d", old->generation);
-					my_syslog(buf);
-				}
-				delete old; // destructor does all the work
-				old_configs.erase(i++);
-			}
-			else i++;
-		}
 	}
 	return NULL;
 }