From dba867ac672af353c776f0665cfb9f3df194f528 Mon Sep 17 00:00:00 2001 From: lidwchn Date: Tue, 18 Jan 2022 10:32:07 +0800 Subject: [PATCH] Solve memory leak issue. Signed-off-by: lidwchn --- ...te_single_ver_relational_continue_token.cpp | 2 +- .../storage/src/sqlite/sqlite_utils.cpp | 2 +- .../distributeddb_relational_get_data_test.cpp | 18 +++++++++++------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_continue_token.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_continue_token.cpp index 6e6192733..bc2150f84 100644 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_continue_token.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_relational_continue_token.cpp @@ -19,7 +19,7 @@ namespace DistributedDB { SQLiteSingleVerRelationalContinueToken::SQLiteSingleVerRelationalContinueToken( const SyncTimeRange &timeRange, const QueryObject &object) - : isGettingDeletedData_(false), queryObj_(object), tableName_(object.GetTableName()), timeRange_(timeRange) + : isGettingDeletedData_(false), queryObj_(object), tableName_(queryObj_.GetTableName()), timeRange_(timeRange) {} bool SQLiteSingleVerRelationalContinueToken::CheckValid() const diff --git a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp index b4f6efaff..e44a55727 100755 --- a/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp +++ b/services/distributeddataservice/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp @@ -1411,7 +1411,7 @@ int SQLiteUtils::CloneIndexes(sqlite3 *db, const std::string &oriTableName, cons { std::string sql = "SELECT 'CREATE ' || CASE WHEN il.'unique' THEN 'UNIQUE ' ELSE '' END || 'INDEX ' || '" + - DBConstant::RELATIONAL_PREFIX + "' || il.name || ' ON ' || '" + newTableName + + newTableName + "_' || il.name || ' ON ' || '" + newTableName + "' || '(' || GROUP_CONCAT(ii.name) || ');' " "FROM sqlite_master AS m," "pragma_index_list(m.name) AS il," diff --git a/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_get_data_test.cpp b/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_get_data_test.cpp index f0e0baa1f..503e0d5d0 100644 --- a/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_get_data_test.cpp +++ b/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_get_data_test.cpp @@ -45,6 +45,7 @@ string g_storeID = "dftStoreID"; const string g_tableName { "data" }; DistributedDB::RelationalStoreManager g_mgr(APP_ID, USER_ID); RelationalStoreDelegate *g_delegate = nullptr; +IRelationalStore *g_store = nullptr; void CreateDBAndTable() { @@ -117,8 +118,8 @@ int GetLogData(int key, uint64_t &flag, TimeStamp ×tamp, const DeviceID &de } END: - sqlite3_close(db); SQLiteUtils::ResetStatement(statement, true, errCode); + sqlite3_close(db); return errCode; } @@ -139,12 +140,12 @@ const RelationalSyncAbleStorage *GetRelationalStore() RelationalDBProperties properties; InitStoreProp(g_storePath, APP_ID, USER_ID, properties); int errCode = E_OK; - auto store = RelationalStoreInstance::GetDataBase(properties, errCode); - if (store == nullptr) { + g_store = RelationalStoreInstance::GetDataBase(properties, errCode); + if (g_store == nullptr) { LOGE("Get db failed:%d", errCode); return nullptr; } - return static_cast(store)->GetStorageEngine(); + return static_cast(g_store)->GetStorageEngine(); } int GetCount(sqlite3 *db, const string &sql, size_t &count) @@ -260,7 +261,7 @@ HWTEST_F(DistributedDBRelationalGetDataTest, GetSyncData1, TestSize.Level1) * @tc.expected: Succeed and the count is right. */ auto store = GetRelationalStore(); - ASSERT_EQ(store, nullptr); + ASSERT_NE(store, nullptr); ContinueToken token = nullptr; QueryObject query(Query::Select(g_tableName)); std::vector entries; @@ -277,6 +278,7 @@ HWTEST_F(DistributedDBRelationalGetDataTest, GetSyncData1, TestSize.Level1) EXPECT_TRUE(errCode == E_OK || errCode == -E_UNFINISHED); } EXPECT_EQ(count, RECORD_COUNT); + RefObject::DecObjRef(g_store); } /** @@ -306,7 +308,7 @@ HWTEST_F(DistributedDBRelationalGetDataTest, GetQuerySyncData1, TestSize.Level1) * @tc.expected: Get 70 records. */ auto store = GetRelationalStore(); - ASSERT_EQ(store, nullptr); + ASSERT_NE(store, nullptr); ContinueToken token = nullptr; const unsigned int LIMIT = 80; // limit as 80. const unsigned int OFFSET = 30; // offset as 30. @@ -319,6 +321,7 @@ HWTEST_F(DistributedDBRelationalGetDataTest, GetQuerySyncData1, TestSize.Level1) EXPECT_EQ(errCode, E_OK); EXPECT_EQ(token, nullptr); SingleVerKvEntry::Release(entries); + RefObject::DecObjRef(g_store); } /** @@ -374,7 +377,7 @@ HWTEST_F(DistributedDBRelationalGetDataTest, GetIncorrectTypeData1, TestSize.Lev * @tc.expected: Succeed and the count is right. */ auto store = GetRelationalStore(); - ASSERT_EQ(store, nullptr); + ASSERT_NE(store, nullptr); ContinueToken token = nullptr; QueryObject query(Query::Select(tableName)); std::vector entries; @@ -411,5 +414,6 @@ HWTEST_F(DistributedDBRelationalGetDataTest, GetIncorrectTypeData1, TestSize.Lev EXPECT_EQ(GetCount(db, sql, count), E_OK); EXPECT_EQ(count, 2UL); // The index count is 2. sqlite3_close(db); + RefObject::DecObjRef(g_store); } #endif \ No newline at end of file -- Gitee