sensor-hal-sensorhub: fix the bug caused by static member variable 34/59234/5
authorkibak.yoon <kibak.yoon@samsung.com>
Thu, 11 Feb 2016 14:19:25 +0000 (23:19 +0900)
committerkibak.yoon <kibak.yoon@samsung.com>
Fri, 12 Feb 2016 02:13:16 +0000 (11:13 +0900)
- static member variable has to be declared for using it.
  strangely std::vector variable is always okay regardless of a compile
  order if it is declared once anywhere. but std::map is not okay.
  dlopen() invokes the segmentation error when std::map variable is used.
  but if compile file order is changed, it doesn't have problem....
- because the problem about initializing static member variable, it is
  issued. so this patch just changes manager/controller to singleton class
  for removing the code that use static variables.
- now it is fixed.

Change-Id: I84777861e5afc374a2ceecbd4d420a90c8f2c1ef
Signed-off-by: kibak.yoon <kibak.yoon@samsung.com>
src/sensorhub/sensorhub.cpp
src/sensorhub/sensorhub_controller.cpp
src/sensorhub/sensorhub_controller.h
src/sensorhub/sensorhub_manager.cpp
src/sensorhub/sensorhub_manager.h
src/sensorhub/wristup.cpp

index 1d522e4..8b5f231 100644 (file)
 
 sensorhub_device::sensorhub_device()
 {
-       controller = new(std::nothrow) sensorhub_controller();
+       controller = &sensorhub_controller::get_instance();
        if (!controller) {
                ERR("Failed to allocated memory");
                throw;
        }
 
-       manager = new(std::nothrow) sensorhub_manager();
+       manager = &sensorhub_manager::get_instance();
        if (!manager) {
                ERR("Failed to allocated memory");
                throw;
@@ -44,9 +44,6 @@ sensorhub_device::sensorhub_device()
 
 sensorhub_device::~sensorhub_device()
 {
-       delete controller;
-       delete manager;
-
        INFO("sensorhub_device is destroyed!");
 }
 
index 53c5a2f..9d7b7fb 100644 (file)
@@ -39,6 +39,12 @@ sensorhub_controller::~sensorhub_controller()
 {
 }
 
+sensorhub_controller& sensorhub_controller::get_instance(void)
+{
+       static sensorhub_controller instance;
+       return instance;
+}
+
 int sensorhub_controller::get_poll_fd(void)
 {
        return -1;
index 67dd9a8..b80aff0 100644 (file)
@@ -22,7 +22,7 @@
 
 class sensorhub_controller {
 public:
-       sensorhub_controller();
+       static sensorhub_controller& get_instance();
        virtual ~sensorhub_controller();
 
        int open_input_node(const char* input_node);
@@ -38,6 +38,8 @@ public:
        int send_sensorhub_data(const char *data, int data_len);
 
 private:
+       sensorhub_controller();
+
        bool m_enabled;
        int m_poll_node;
        int m_data_node;
index a955fc6..838d169 100644 (file)
 #include "sensorhub_manager.h"
 #include "sensorhub_sensor.h"
 
-std::map<uint16_t, sensorhub_sensor *> sensorhub_manager::m_id_sensor;
-std::map<char, sensorhub_sensor *> sensorhub_manager::m_key_sensor;
-std::vector<sensor_handle_t> sensorhub_manager::m_handles;
-
 sensorhub_manager::sensorhub_manager()
 {
 }
@@ -37,6 +33,11 @@ sensorhub_manager::~sensorhub_manager()
        m_handles.clear();
 }
 
+sensorhub_manager& sensorhub_manager::get_instance() {
+       static sensorhub_manager instance;
+       return instance;
+}
+
 bool sensorhub_manager::add_sensor(sensor_handle_t handle, char key, sensorhub_sensor *sensor)
 {
        m_handles.push_back(handle);
index 8939144..4e2ab13 100644 (file)
 
 #include <map>
 #include <vector>
+#include <sensor_logs.h>
 
 #include "sensorhub_controller.h"
 #include "sensorhub_sensor.h"
 
-#define REGISTER_SENSOR(handle, key, sensor_class) \
-static void __attribute__((constructor)) add_sensorhub_sensor(void) \
-{ \
-       sensorhub_sensor *sensor = new(std::nothrow) (sensor_class)(); \
-       if (!sensor) { \
-               _E("ERROR: Failed to allocate memory(%s)", #sensor_class); \
-               return; \
-       } \
-       sensorhub_manager::add_sensor((handle), (key), (sensor)); \
-}
+#define REGISTER_SENSORHUB_LIB(handle, key, sensor_class) \
+       static sensor_initializer<sensor_class> initializer((handle), (key)); \
 
 class sensorhub_manager {
 public:
-       sensorhub_manager();
+       static sensorhub_manager& get_instance();
        virtual ~sensorhub_manager();
 
        sensorhub_sensor *get_sensor(uint16_t id);
@@ -45,11 +38,28 @@ public:
        int get_sensors(const sensor_handle_t **sensors);
 
        void set_controller(sensorhub_controller *controller);
-
-       static bool add_sensor(sensor_handle_t handle, char key, sensorhub_sensor *sensor);
+       bool add_sensor(sensor_handle_t handle, char key, sensorhub_sensor *sensor);
 private:
-       static std::map<uint16_t, sensorhub_sensor *> m_id_sensor;
-       static std::map<char, sensorhub_sensor *> m_key_sensor;
-       static std::vector<sensor_handle_t> m_handles;
+       sensorhub_manager();
+
+       std::map<uint16_t, sensorhub_sensor *> m_id_sensor;
+       std::map<char, sensorhub_sensor *> m_key_sensor;
+       std::vector<sensor_handle_t> m_handles;
 };
+
+template <typename T>
+class sensor_initializer {
+public:
+       sensor_initializer(sensor_handle_t handle, char key)
+       {
+               T *sensor = new(std::nothrow) T();
+               if (!sensor) {
+                       ERR("Failed to allocate memory");
+                       return;
+               }
+               sensorhub_manager::get_instance().add_sensor(handle, key, sensor);
+       }
+       ~sensor_initializer() {}
+};
+
 #endif /* _SENSORHUB_MANAGER_H_ */
index 0a9bdae..f26dfd7 100644 (file)
@@ -86,4 +86,4 @@ bool wristup_sensor::set_attribute_str(char *key, char *value, int value_len)
        return false;
 }
 
-REGISTER_SENSOR(handle, (char)SHUB_LIB_WRIST_UP, wristup_sensor)
+REGISTER_SENSORHUB_LIB(handle, (char)SHUB_LIB_WRIST_UP, wristup_sensor)