This is an automated email from the ASF dual-hosted git repository.
haonan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new 9cbb7738ce6 fix cpp session mem leak (#16443)
9cbb7738ce6 is described below
commit 9cbb7738ce6074c094701c4194e7214e124febcb
Author: Hongzhi Gao <[email protected]>
AuthorDate: Fri Sep 19 11:43:35 2025 +0800
fix cpp session mem leak (#16443)
---
example/client-cpp-example/src/TableModelSessionExample.cpp | 4 +---
iotdb-client/client-cpp/src/main/SessionBuilder.h | 6 +++---
iotdb-client/client-cpp/src/main/TableSession.h | 12 ++++--------
iotdb-client/client-cpp/src/main/TableSessionBuilder.h | 6 +++---
4 files changed, 11 insertions(+), 17 deletions(-)
diff --git a/example/client-cpp-example/src/TableModelSessionExample.cpp
b/example/client-cpp-example/src/TableModelSessionExample.cpp
index 4e4f9274342..7546f97d970 100644
--- a/example/client-cpp-example/src/TableModelSessionExample.cpp
+++ b/example/client-cpp-example/src/TableModelSessionExample.cpp
@@ -22,7 +22,7 @@
using namespace std;
-TableSession *session;
+shared_ptr<TableSession> session;
void insertRelationalTablet() {
@@ -203,8 +203,6 @@ int main() {
cout << "session close\n" << endl;
session->close();
- delete session;
-
cout << "finished!\n" << endl;
} catch (IoTDBConnectionException &e) {
cout << e.what() << endl;
diff --git a/iotdb-client/client-cpp/src/main/SessionBuilder.h
b/iotdb-client/client-cpp/src/main/SessionBuilder.h
index d1b11035609..9eb66e7b600 100644
--- a/iotdb-client/client-cpp/src/main/SessionBuilder.h
+++ b/iotdb-client/client-cpp/src/main/SessionBuilder.h
@@ -79,12 +79,12 @@ public:
return this;
}
- Session* build() {
+ std::shared_ptr<Session> build() {
sqlDialect = "tree";
- Session* newSession = new Session(this);
+ auto newSession = std::make_shared<Session>(this);
newSession->open(false);
return newSession;
- }
+ }
};
#endif // IOTDB_SESSION_BUILDER_H
\ No newline at end of file
diff --git a/iotdb-client/client-cpp/src/main/TableSession.h
b/iotdb-client/client-cpp/src/main/TableSession.h
index 4829c1ccd9b..09768439e83 100644
--- a/iotdb-client/client-cpp/src/main/TableSession.h
+++ b/iotdb-client/client-cpp/src/main/TableSession.h
@@ -26,18 +26,14 @@
class TableSession {
private:
- Session* session_;
+ std::shared_ptr<Session> session_;
string getDatabase();
public:
- TableSession(Session* session) {
+ TableSession(std::shared_ptr<Session> session) {
this->session_ = session;
}
- ~TableSession() {
- if (session_) {
- delete session_;
- session_ = nullptr;
- }
- }
+ ~TableSession() {}
+
void insert(Tablet& tablet, bool sorted = false);
void executeNonQueryStatement(const std::string& sql);
unique_ptr<SessionDataSet> executeQueryStatement(const std::string& sql);
diff --git a/iotdb-client/client-cpp/src/main/TableSessionBuilder.h
b/iotdb-client/client-cpp/src/main/TableSessionBuilder.h
index db6c749d184..7d58fc87073 100644
--- a/iotdb-client/client-cpp/src/main/TableSessionBuilder.h
+++ b/iotdb-client/client-cpp/src/main/TableSessionBuilder.h
@@ -69,11 +69,11 @@ public:
AbstractSessionBuilder::nodeUrls = nodeUrls;
return this;
}
- TableSession* build() {
+ std::shared_ptr<TableSession> build() {
sqlDialect = "table";
- Session* newSession = new Session(this);
+ auto newSession = std::make_shared<Session>(this);
newSession->open(false);
- return new TableSession(newSession);
+ return std::make_shared<TableSession>(newSession);
}
};