From 14ca6b205211e856c924f88dff3abf0584b1e414 Mon Sep 17 00:00:00 2001 From: Mika Laitio Date: Fri, 11 Mar 2011 17:57:56 +0200 Subject: [PATCH] Memory leak fixes. Signed-off-by: Mika Laitio --- src/Factory.cc | 7 +++++- src/W1Device.cc | 41 ++++++++++++++++++++++--------- src/W1Device.hh | 10 +++++--- src/W1TemperatureSensor.cc | 1 + src_test/test_w1_datalog_read.cc | 12 ++++++--- src_test/test_w1_datalog_write.cc | 2 ++ 6 files changed, 53 insertions(+), 20 deletions(-) diff --git a/src/Factory.cc b/src/Factory.cc index 93d7262..e785683 100644 --- a/src/Factory.cc +++ b/src/Factory.cc @@ -159,13 +159,13 @@ Device *Factory::create_w1_device(dirent *direntry_param, int *err_code_param) { } list Factory::get_device_list() { + list ret_val; DIR *dir; int err_flg; struct dirent *direntry; Device *device; bool is_subdir; list rdr_list; - list ret_val; list::iterator dev_iter; list::iterator rdr_iter; DataReader *reader; @@ -221,6 +221,11 @@ list Factory::get_device_list() { ret_val.push_back(device); } } + while(rdr_list.empty() == false) { + reader = rdr_list.back(); + rdr_list.pop_back(); + delete(reader); + } return ret_val; } diff --git a/src/W1Device.cc b/src/W1Device.cc index f2e8590..4a83c20 100644 --- a/src/W1Device.cc +++ b/src/W1Device.cc @@ -29,6 +29,7 @@ W1Device::W1Device(string device_id_param, string device_dir; string temp_str; + pthread_mutex_init(&plock, NULL); type = device_type_param; id = device_id_param; if (direntry_param != NULL) { @@ -47,8 +48,12 @@ W1Device::W1Device(string device_id_param, } W1Device::~W1Device() { + log_debug("W1Device destructor\n"); save_and_clean_cache(); - delete(reader); + if (reader != NULL) { + delete(reader); + reader = NULL; + } } string W1Device::get_name() { @@ -87,6 +92,7 @@ void W1Device::printout() { if (data != NULL) { text = data->to_string(); cout << text << endl; + delete(data); } else { type = get_type(); @@ -127,28 +133,41 @@ Data *W1Device::get_data() { return ret_val; } -void W1Device::cache(Data *data) { - // TODO: add mutex for memory_cache +void W1Device::cache(Data *new_data) { + Data *data; + int dec_precision; + + data = new_data->clone(); + pthread_mutex_lock(&plock); memory_cache.push_back(data); if (memory_cache.size() > 5) { - save_and_clean_cache(); + dec_precision = get_data_decimal_precision(); + StoreDay::save(id, &memory_cache, dec_precision); + while(memory_cache.empty() == false) { + data = memory_cache.back(); + memory_cache.pop_back(); + delete(data); + } } + pthread_mutex_unlock(&plock); } void W1Device::save_and_clean_cache() { - list::iterator iter; - Data *data; - int dec_precision; + Data *data; + int dec_precision; dec_precision = get_data_decimal_precision(); + pthread_mutex_lock(&plock); + log_debug("save_and_clean_cache(): memory cache size: %d\n", memory_cache.size()); StoreDay::save(id, &memory_cache, dec_precision); - for(iter = memory_cache.begin(); iter != memory_cache.end(); iter++) { - data = (Data *)*iter; + while(memory_cache.empty() == false) { + data = memory_cache.back(); + memory_cache.pop_back(); delete(data); } - memory_cache.clear(); + pthread_mutex_unlock(&plock); } -DataReader *W1Device::get_device_data() { +const DataReader *W1Device::get_device_data() { return reader; } diff --git a/src/W1Device.hh b/src/W1Device.hh index a930303..28d4d38 100644 --- a/src/W1Device.hh +++ b/src/W1Device.hh @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -33,12 +34,12 @@ namespace w1 { W1Device(std::string device_id_param, std::string device_type_param, dirent *direntry_param); - virtual ~W1Device(); + ~W1Device(); std::string get_name(); void set_name(std::string name_param); void printout(); plp::Data *get_data(); - plp::DataReader *get_device_data(); + const plp::DataReader *get_device_data(); protected: std::string dir_path; std::string slave_file; @@ -46,9 +47,10 @@ namespace w1 { plp::DataReader *reader; void save_and_clean_cache(); virtual std::vector *get_raw_data() = 0; - virtual unsigned int get_data_decimal_precision() = 0; - void cache(plp::Data *data); + virtual unsigned int get_data_decimal_precision() { return 1; } + void cache(plp::Data *new_data); std::string to_string(double val, int digit_count); + pthread_mutex_t plock; }; } diff --git a/src/W1TemperatureSensor.cc b/src/W1TemperatureSensor.cc index 4aa52da..152125a 100644 --- a/src/W1TemperatureSensor.cc +++ b/src/W1TemperatureSensor.cc @@ -62,6 +62,7 @@ W1TemperatureSensor::W1TemperatureSensor(string device_id_param, } W1TemperatureSensor::~W1TemperatureSensor() { + log_debug("W1TemperatureSensor destructor\n"); } vector *W1TemperatureSensor::get_raw_data() { diff --git a/src_test/test_w1_datalog_read.cc b/src_test/test_w1_datalog_read.cc index 48425e2..f833af5 100644 --- a/src_test/test_w1_datalog_read.cc +++ b/src_test/test_w1_datalog_read.cc @@ -51,7 +51,7 @@ int main(int argc, char** argv) { string loc; Data *fdata; Data *ldata; - DataReader *reader; + const DataReader *reader; Device *dev; SensorDevice *device; DataRange *dr; @@ -77,7 +77,7 @@ int main(int argc, char** argv) { if (device != NULL) { reader = device->get_device_data(); if (reader != NULL) { - dr = reader->get_data_range(); + dr = ((DataReader *)reader)->get_data_range(); if (dr != NULL) { fdata = dr->get_first(); if (fdata != NULL) { @@ -90,7 +90,7 @@ int main(int argc, char** argv) { d1 = fdata->get_date(); d2 = ldata->get_date(); - dr2 = reader->get_data(&d1, &d2); + dr2 = ((DataReader *)reader)->get_data(&d1, &d2); if (dr2 != NULL) { dr2->printout(); delete(dr2); @@ -101,10 +101,14 @@ int main(int argc, char** argv) { } delete(dr); } - delete(reader); } } } } + while (dev_lst.empty() == false) { + dev = dev_lst.back(); + dev_lst.pop_back(); + delete(dev); + } return 0; } diff --git a/src_test/test_w1_datalog_write.cc b/src_test/test_w1_datalog_write.cc index 45e8036..748b9fe 100644 --- a/src_test/test_w1_datalog_write.cc +++ b/src_test/test_w1_datalog_write.cc @@ -117,9 +117,11 @@ int main(int argc, char** argv) { else { log_debug("could not find 1-wire devices.\n"); } + log_debug("trying to start emptying list\n"); while (device_list.empty() == false) { device = device_list.back(); device_list.pop_back(); + log_debug("calling delete\n"); delete(device); } return 0; -- 2.41.1