Fix memory leak
[platform/core/uifw/tts.git] / server / ttsd_data.cpp
index 1056101..47e335b 100644 (file)
@@ -44,6 +44,7 @@ static vector<app_data_s> g_app_list;
 static pthread_mutex_t g_speak_data_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t g_sound_data_mutex = PTHREAD_MUTEX_INITIALIZER;
 
+
 /* If engine is running */
 static ttsd_synthesis_control_e        g_synth_control;
 
@@ -363,6 +364,27 @@ int ttsd_data_add_speak_data(int uid, speak_data_s* data)
        return TTSD_ERROR_NONE;
 }
 
+int __get_speak_data(int index, speak_data_s** data)
+{
+       if (0 == g_app_list[index].m_speak_data.size()) {
+#ifdef DATA_DEBUG
+               SLOG(LOG_WARN, tts_tag(), "[DATA WARNING] There is no speak data");
+#endif
+               return -1;
+       }
+
+       if (!g_app_list[index].m_speak_data.empty()) {
+               std::list<speak_data_s*>::iterator iter = g_app_list[index].m_speak_data.begin();
+               *data = *iter;
+               g_app_list[index].m_speak_data.pop_front();
+       }
+
+#ifdef DATA_DEBUG
+       __data_show_text_list(index);
+#endif
+       return 0;
+}
+
 int ttsd_data_get_speak_data(int uid, speak_data_s** data)
 {
        int index = 0;
@@ -376,23 +398,12 @@ int ttsd_data_get_speak_data(int uid, speak_data_s** data)
        /* mutex is locked */
        pthread_mutex_lock(&g_speak_data_mutex);
 
-       if (0 == g_app_list[index].m_speak_data.size()) {
-#ifdef DATA_DEBUG
+       if (0 != __get_speak_data(index, data)) {
                SLOG(LOG_WARN, tts_tag(), "[DATA WARNING] There is no speak data");
-#endif
                pthread_mutex_unlock(&g_speak_data_mutex);
                return -1;
        }
 
-       if (!g_app_list[index].m_speak_data.empty()) {
-               std::list<speak_data_s*>::iterator iter = g_app_list[index].m_speak_data.begin();
-               *data = *iter;
-               g_app_list[index].m_speak_data.pop_front();
-       }
-
-#ifdef DATA_DEBUG
-       __data_show_text_list(index);
-#endif
        pthread_mutex_unlock(&g_speak_data_mutex);
 
        return TTSD_ERROR_NONE;
@@ -437,6 +448,27 @@ int ttsd_data_add_sound_data(int uid, sound_data_s* data)
        return TTSD_ERROR_NONE;
 }
 
+int __get_sound_data(int index, sound_data_s** data)
+{
+       if (0 == g_app_list[index].m_wav_data.size()) {
+#ifdef DATA_DEBUG
+               SLOG(LOG_DEBUG, tts_tag(), "[DATA] There is no wav data");
+#endif
+               return -1;
+       }
+
+       if (!g_app_list[index].m_wav_data.empty()) {
+               std::list<sound_data_s*>::iterator iter = g_app_list[index].m_wav_data.begin();
+               *data = *iter;
+               g_app_list[index].m_wav_data.pop_front();
+       }
+
+#ifdef DATA_DEBUG
+       __data_show_sound_list(index);
+#endif
+       return 0;
+}
+
 int ttsd_data_get_sound_data(int uid, sound_data_s** data)
 {
        int index = 0;
@@ -452,25 +484,12 @@ int ttsd_data_get_sound_data(int uid, sound_data_s** data)
        /* mutex is locked */
        pthread_mutex_lock(&g_sound_data_mutex);
 
-       if (0 == g_app_list[index].m_wav_data.size()) {
-#ifdef DATA_DEBUG
+       if (0 != __get_sound_data(index, data)) {
                SLOG(LOG_DEBUG, tts_tag(), "[DATA] There is no wav data");
-#endif
                /* mutex is unlocked */
                pthread_mutex_unlock(&g_sound_data_mutex);
                return -1;
        }
-
-       if (!g_app_list[index].m_wav_data.empty()) {
-               std::list<sound_data_s*>::iterator iter = g_app_list[index].m_wav_data.begin();
-               *data = *iter;
-               g_app_list[index].m_wav_data.pop_front();
-       }
-
-#ifdef DATA_DEBUG
-       __data_show_sound_list(index);
-#endif
-
        /* mutex is unlocked */
        pthread_mutex_unlock(&g_sound_data_mutex);
 
@@ -498,25 +517,29 @@ int ttsd_data_get_sound_data_size(int uid)
        return data_size;
 }
 
-int ttsd_data_clear_speak_data(speak_data_s** speak_data)
+int ttsd_data_clear_speak_data(int uid, speak_data_s** speak_data)
 {
        pthread_mutex_lock(&g_speak_data_mutex);
 
-       if (NULL != *speak_data) {
-               SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] utt(%d), text(%s), lang(%s), vctype(%d) speed(%d)", 
-                               (*speak_data)->utt_id, (*speak_data)->text, (*speak_data)->lang, (*speak_data)->vctype, (*speak_data)->speed);
+       int index = 0;
+       index = ttsd_data_is_client(uid);
+       if (index >= 0) {
+               if (NULL != *speak_data) {
+                       SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] utt(%d), text(%s), lang(%s), vctype(%d) speed(%d)", 
+                                       (*speak_data)->utt_id, (*speak_data)->text, (*speak_data)->lang, (*speak_data)->vctype, (*speak_data)->speed);
 
-               if (NULL != (*speak_data)->text) {
-                       free((*speak_data)->text);
-                       (*speak_data)->text = NULL;
-               }
-               if (NULL != (*speak_data)->lang) {
-                       free((*speak_data)->lang);
-                       (*speak_data)->lang = NULL;
-               }
+                       if (NULL != (*speak_data)->text) {
+                               free((*speak_data)->text);
+                               (*speak_data)->text = NULL;
+                       }
+                       if (NULL != (*speak_data)->lang) {
+                               free((*speak_data)->lang);
+                               (*speak_data)->lang = NULL;
+                       }
 
-               free(*speak_data);
-               *speak_data = NULL;
+                       free(*speak_data);
+                       *speak_data = NULL;
+               }
        }
 
        pthread_mutex_unlock(&g_speak_data_mutex);
@@ -524,23 +547,26 @@ int ttsd_data_clear_speak_data(speak_data_s** speak_data)
        return TTSD_ERROR_NONE;
 }
 
-int ttsd_data_clear_sound_data(sound_data_s** sound_data)
+int ttsd_data_clear_sound_data(int uid, sound_data_s** sound_data)
 {
        pthread_mutex_lock(&g_sound_data_mutex);
 
-       if (NULL != *sound_data) {
-               SLOG(LOG_ERROR, tts_tag(), "[DEBUG][%p] event(%d) data(%p) size(%d) rate(%d) utt(%d)", 
-                       (*sound_data), (*sound_data)->event, (*sound_data)->data, (*sound_data)->data_size, (*sound_data)->rate, (*sound_data)->utt_id);
+       int index = 0;
+       index = ttsd_data_is_client(uid);
+       if (index >= 0) {
+               if (NULL != *sound_data) {
+                       SLOG(LOG_ERROR, tts_tag(), "[DEBUG][%p] event(%d) data(%p) size(%d) rate(%d) utt(%d)", 
+                                       (*sound_data), (*sound_data)->event, (*sound_data)->data, (*sound_data)->data_size, (*sound_data)->rate, (*sound_data)->utt_id);
+
+                       if (NULL != (*sound_data)->data) {
+                               free((*sound_data)->data);
+                               (*sound_data)->data = NULL;
+                       }
 
-               if (NULL != (*sound_data)->data) {
-                       free((*sound_data)->data);
-                       (*sound_data)->data = NULL;
+                       free(*sound_data);
+                       *sound_data = NULL;
                }
-
-               free(*sound_data);
-               *sound_data = NULL;
        }
-
        pthread_mutex_unlock(&g_sound_data_mutex);
 
        return TTSD_ERROR_NONE;
@@ -561,12 +587,12 @@ int ttsd_data_clear_data(int uid)
        sound_data_s* temp_sound = NULL;
 
        /* free allocated data */
+       pthread_mutex_lock(&g_speak_data_mutex);
        while(1) {
-               if (0 != ttsd_data_get_speak_data(uid, &temp_speak)) {
+               if (0 != __get_speak_data(index, &temp_speak)) {
                        break;
                }
 
-               pthread_mutex_lock(&g_speak_data_mutex);
                if (NULL != temp_speak) {
                        SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] utt(%d), text(%s), lang(%s), vctype(%d) speed(%d)", 
                                        temp_speak->utt_id, temp_speak->text, temp_speak->lang, temp_speak->vctype, temp_speak->speed);
@@ -584,23 +610,21 @@ int ttsd_data_clear_data(int uid)
                        free(temp_speak);
                        temp_speak = NULL;
                }
-               pthread_mutex_unlock(&g_speak_data_mutex);
        }
 
        if (-1 != removed_last_uttid) {
                g_app_list[index].utt_id_stopped = removed_last_uttid;
        }
 
-       pthread_mutex_lock(&g_speak_data_mutex);
        g_app_list[index].m_speak_data.clear();
        pthread_mutex_unlock(&g_speak_data_mutex);
 
+       pthread_mutex_lock(&g_sound_data_mutex);
        while(1) {
-               if (0 != ttsd_data_get_sound_data(uid, &temp_sound)) {
+               if (0 != __get_sound_data(index, &temp_sound)) {
                        break;
                }
 
-               pthread_mutex_lock(&g_sound_data_mutex);
                if (NULL != temp_sound) {
                        SLOG(LOG_ERROR, tts_tag(), "[DEBUG][%p] uid(%d), event(%d) data(%p) size(%d) rate(%d) utt(%d)", 
                                temp_sound, uid, temp_sound->event, temp_sound->data, temp_sound->data_size, temp_sound->rate, temp_sound->utt_id);
@@ -613,10 +637,8 @@ int ttsd_data_clear_data(int uid)
                        free(temp_sound);
                        temp_sound = NULL;
                }
-               pthread_mutex_unlock(&g_sound_data_mutex);
        }
 
-       pthread_mutex_lock(&g_sound_data_mutex);
        g_app_list[index].m_wav_data.clear();
        pthread_mutex_unlock(&g_sound_data_mutex);