]> pilppa.com Git - lib1wire.git/commitdiff
Memory leak fixes.
authorMika Laitio <lamikr@pilppa.org>
Fri, 11 Mar 2011 15:57:56 +0000 (17:57 +0200)
committerMika Laitio <lamikr@pilppa.org>
Fri, 11 Mar 2011 15:57:56 +0000 (17:57 +0200)
Signed-off-by: Mika Laitio <lamikr@pilppa.org>
src/Factory.cc
src/W1Device.cc
src/W1Device.hh
src/W1TemperatureSensor.cc
src_test/test_w1_datalog_read.cc
src_test/test_w1_datalog_write.cc

index 93d72628d62ac6f7cafcf9e5fc985ba54be95070..e785683dcbbc21e09bbdb29099ecdb756a53c195 100644 (file)
@@ -159,13 +159,13 @@ Device *Factory::create_w1_device(dirent *direntry_param, int *err_code_param) {
 }
 
 list<Device *> Factory::get_device_list() {
+       list<Device *>                  ret_val;
        DIR                             *dir;
        int                             err_flg;
        struct dirent                   *direntry;
        Device                          *device;
        bool                            is_subdir;
        list<DataReader *>              rdr_list;
-       list<Device *>                  ret_val;
        list<Device *>::iterator        dev_iter;
        list<DataReader *>::iterator    rdr_iter;
        DataReader                      *reader;
@@ -221,6 +221,11 @@ list<Device *> 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;
 }
 
index f2e85902c0c6e3afa1b75085beda15e5ba27c063..4a83c20b95970f0f352e8d95391910f0827841a5 100644 (file)
@@ -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<Data *>::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;
 }
index a9303030859cc5c0713ca48259f2cab36f4129b9..28d4d3852cf217fc17159e1a04698128e4ac0387 100644 (file)
@@ -13,6 +13,7 @@
 
 #include <dirent.h>
 #include <stdbool.h>
+#include <pthread.h>
 
 #include <plp/Data.hh>
 #include <plp/DataReader.hh>
@@ -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<double> *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;
        };
 }
 
index 4aa52dab6e2514cfa6f4922bee80a72ce6e60c54..152125a1d34672817f0910e4b59461c307d09dfd 100644 (file)
@@ -62,6 +62,7 @@ W1TemperatureSensor::W1TemperatureSensor(string device_id_param,
 }
 
 W1TemperatureSensor::~W1TemperatureSensor() {
+       log_debug("W1TemperatureSensor destructor\n");
 }
 
 vector<double> *W1TemperatureSensor::get_raw_data() {
index 48425e21b342069775512f050fcaeb0bc9c2d85d..f833af5caeda0346ad3600d893f3baee52b28c10 100644 (file)
@@ -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;
 }
index 45e803683d8627cb796fa77280537ddfe8fc574a..748b9fe29ea2eb14f4b65e648405dd432a5b16a3 100644 (file)
@@ -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;