From 755dd7901fc01957fba2667c4cb0510677861e24 Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Fri, 5 Jul 2019 16:13:35 +0200 Subject: [PATCH] Add copy constructor, move constructor and move assignment operator. Fix memory leak --- src/algorithms/PVT/libs/serdes_monitor_pvt.h | 33 +++++++++++++++++++- src/core/monitor/serdes_gnss_synchro.h | 33 +++++++++++++++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/algorithms/PVT/libs/serdes_monitor_pvt.h b/src/algorithms/PVT/libs/serdes_monitor_pvt.h index 490580818..a03e297db 100644 --- a/src/algorithms/PVT/libs/serdes_monitor_pvt.h +++ b/src/algorithms/PVT/libs/serdes_monitor_pvt.h @@ -48,7 +48,6 @@ public: // Verify that the version of the library that we linked against is // compatible with the version of the headers we compiled against. GOOGLE_PROTOBUF_VERIFY_VERSION; - monitor_.New(); } ~Serdes_Monitor_Pvt() @@ -56,6 +55,36 @@ public: // google::protobuf::ShutdownProtobufLibrary(); } + inline Serdes_Monitor_Pvt(Serdes_Monitor_Pvt&& other) //!< Copy constructor + { + this->monitor_ = other.monitor_; + } + + inline Serdes_Monitor_Pvt& operator=(const Serdes_Monitor_Pvt& rhs) //!< Copy assignment operator + { + // Only do assignment if RHS is a different object from this. + if (this != &rhs) + { + // Deallocate, allocate new space, copy values... + this->monitor_ = rhs.monitor_; + } + return *this; + } + + inline Serdes_Monitor_Pvt(const Serdes_Monitor_Pvt& other) //!< Move constructor + { + this->monitor_ = std::move(other.monitor_); + } + + inline Serdes_Monitor_Pvt& operator=(Serdes_Monitor_Pvt&& other) //!< Move assignment operator + { + if (this != &other) + { + this->monitor_ = std::move(other.monitor_); + } + return *this; + } + inline std::string createProtobuffer(const Monitor_Pvt& monitor) //!< Serialization into a string { monitor_.Clear(); @@ -91,6 +120,8 @@ public: monitor_.set_hdop(monitor.hdop); monitor_.set_vdop(monitor.vdop); + monitor_.CheckInitialized(); + monitor_.SerializeToString(&data); return data; } diff --git a/src/core/monitor/serdes_gnss_synchro.h b/src/core/monitor/serdes_gnss_synchro.h index 0e63aed6c..39ececdab 100644 --- a/src/core/monitor/serdes_gnss_synchro.h +++ b/src/core/monitor/serdes_gnss_synchro.h @@ -50,7 +50,6 @@ public: // Verify that the version of the library that we linked against is // compatible with the version of the headers we compiled against. GOOGLE_PROTOBUF_VERIFY_VERSION; - observables.New(); } ~Serdes_Gnss_Synchro() @@ -58,6 +57,36 @@ public: google::protobuf::ShutdownProtobufLibrary(); } + inline Serdes_Gnss_Synchro(Serdes_Gnss_Synchro&& other) //!< Copy constructor + { + this->observables = other.observables; + } + + inline Serdes_Gnss_Synchro& operator=(const Serdes_Gnss_Synchro& rhs) //!< Copy assignment operator + { + // Only do assignment if RHS is a different object from this. + if (this != &rhs) + { + // Deallocate, allocate new space, copy values... + this->observables = rhs.observables; + } + return *this; + } + + inline Serdes_Gnss_Synchro(const Serdes_Gnss_Synchro& other) //!< Move constructor + { + this->observables = std::move(other.observables); + } + + inline Serdes_Gnss_Synchro& operator=(Serdes_Gnss_Synchro&& other) //!< Move assignment operator + { + if (this != &other) + { + this->observables = std::move(other.observables); + } + return *this; + } + inline std::string createProtobuffer(const std::vector& vgs) //!< Serialization into a string { observables.Clear(); @@ -101,6 +130,8 @@ public: obs->set_rx_time(gs.RX_time); obs->set_flag_valid_pseudorange(gs.Flag_valid_pseudorange); obs->set_interp_tow_ms(gs.interp_TOW_ms); + + obs->CheckInitialized(); } observables.SerializeToString(&data); return data;