Fix memory leak when loading test pcm data 48/263048/4
authorSuyeon Hwang <stom.hwang@samsung.com>
Wed, 25 Aug 2021 05:18:30 +0000 (14:18 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Wed, 25 Aug 2021 07:01:33 +0000 (16:01 +0900)
Change-Id: I9769d2cfd14c97c9962cf7da412274e8bd11b2b5
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
tests/src/tts_unittests.cpp

index e11d29e..c01763a 100644 (file)
@@ -33,6 +33,8 @@ static tts_state_e g_current_state;
 static bool g_utterance_started_cb = false;
 static bool g_utterance_completed_cb = false;
 static int g_utt_id = -1;
+static char* g_pcm_data = nullptr;
+static long g_pcm_size = 0;
 static const char *g_text = "Speech Synthesis is the artificial production of human speech. A computer system used for this purpose is called a speech computer or speech synthesizer,\
        and can be implemented in software or hardware products. A text-to-speech (TTS) system converts normal language text into speech; other systems render symbolic linguistic\
        representations like phonetic transcriptions into speech.\
@@ -96,6 +98,37 @@ static bool __is_state_changed(tts_state_e state, int wait_delay)
        return true;
 }
 
+static void __get_test_PCM_Data()
+{
+       const char* pcm_path = tzplatform_mkpath(tzplatform_getid("TZ_SYS_RO_APP"), "/org.tizen.tts-unittests/res/test_pcm.dat");
+       FILE* fp_in = fopen(pcm_path, "rb");
+       if (fp_in == nullptr) {
+               return;
+       }
+
+       fseek(fp_in, 0, SEEK_END);
+       long size = ftell(fp_in);
+       fseek(fp_in, 0, SEEK_SET);
+       if (size <= 0) {
+               fclose(fp_in);
+               return;
+       }
+
+       char* data = (char*)calloc(sizeof(char), size);
+       size_t read_size = fread(data, sizeof(char), size, fp_in);
+       fclose(fp_in);
+
+       if (read_size <= 0) {
+               free(data);
+               return;
+       }
+
+       g_pcm_size = size;
+       g_pcm_data = data;
+
+       return;
+}
+
 namespace {
 
 class TTSTest : public testing::Test {
@@ -137,6 +170,10 @@ class TTSTest : public testing::Test {
                        g_tts = NULL;
                        is_created_hndl = -1;
 
+                       free(g_pcm_data);
+                       g_pcm_data = nullptr;
+                       g_pcm_size = 0;
+
                        ecore_shutdown();
                }
 };
@@ -3134,32 +3171,22 @@ TEST_F(TTSTest, utc_tts_play_pcm_p2)
        EXPECT_EQ(true, __is_state_changed(TTS_STATE_READY, 5));
 
        g_utterance_completed_cb = false;
-       const char* pcm_path = tzplatform_mkpath(tzplatform_getid("TZ_SYS_RO_APP"), "/org.tizen.tts-unittests/res/test_pcm.dat");
-       FILE* fp_in = fopen(pcm_path, "rb");
-       ASSERT_NE(fp_in, nullptr);
-
-       fseek(fp_in, 0, SEEK_END);
-       size_t size = ftell(fp_in);
-       fseek(fp_in, 0, SEEK_SET);
+       __get_test_PCM_Data();
+       ASSERT_NE(g_pcm_data, nullptr);
+       ASSERT_GT(g_pcm_size, 0);
 
        const size_t shift_size = 12000;
-       char* data = (char*)calloc(sizeof(char), size);
-       size_t read_size = fread(data, sizeof(char), size, fp_in);
-       ASSERT_GT(read_size, 0);
-
-       int data_num = size / shift_size;
+       int data_num = g_pcm_size / shift_size;
        for (int i = 0; i <= data_num; i++ ) {
                if (0 == i) {
-                       EXPECT_EQ(tts_add_pcm(g_tts, 1, &data[i*shift_size], shift_size, 0, g_sample_rate), TTS_ERROR_NONE);
+                       EXPECT_EQ(tts_add_pcm(g_tts, 1, &g_pcm_data[i*shift_size], shift_size, 0, g_sample_rate), TTS_ERROR_NONE);
                } else if (data_num == i) {
-                       EXPECT_EQ(tts_add_pcm(g_tts, 3, &data[i*shift_size], size % shift_size, 0, g_sample_rate), TTS_ERROR_NONE);
+                       EXPECT_EQ(tts_add_pcm(g_tts, 3, &g_pcm_data[i*shift_size], g_pcm_size % shift_size, 0, g_sample_rate), TTS_ERROR_NONE);
                } else {
-                       EXPECT_EQ(tts_add_pcm(g_tts, 2, &data[i*shift_size], shift_size, 0, g_sample_rate), TTS_ERROR_NONE);
+                       EXPECT_EQ(tts_add_pcm(g_tts, 2, &g_pcm_data[i*shift_size], shift_size, 0, g_sample_rate), TTS_ERROR_NONE);
                }
        }
 
-       free(data);
-
        EXPECT_EQ(tts_play_pcm(g_tts), TTS_ERROR_NONE);
        EXPECT_EQ(true, __is_state_changed(TTS_STATE_PLAYING, 5));