Skip to content

Commit 9cbb773

Browse files
authored
fix cpp session mem leak (apache#16443)
1 parent dd6f958 commit 9cbb773

File tree

4 files changed

+11
-17
lines changed

4 files changed

+11
-17
lines changed

example/client-cpp-example/src/TableModelSessionExample.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
using namespace std;
2424

25-
TableSession *session;
25+
shared_ptr<TableSession> session;
2626

2727
void insertRelationalTablet() {
2828

@@ -203,8 +203,6 @@ int main() {
203203
cout << "session close\n" << endl;
204204
session->close();
205205

206-
delete session;
207-
208206
cout << "finished!\n" << endl;
209207
} catch (IoTDBConnectionException &e) {
210208
cout << e.what() << endl;

iotdb-client/client-cpp/src/main/SessionBuilder.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ class SessionBuilder : public AbstractSessionBuilder {
7979
return this;
8080
}
8181

82-
Session* build() {
82+
std::shared_ptr<Session> build() {
8383
sqlDialect = "tree";
84-
Session* newSession = new Session(this);
84+
auto newSession = std::make_shared<Session>(this);
8585
newSession->open(false);
8686
return newSession;
87-
}
87+
}
8888
};
8989

9090
#endif // IOTDB_SESSION_BUILDER_H

iotdb-client/client-cpp/src/main/TableSession.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,14 @@
2626

2727
class TableSession {
2828
private:
29-
Session* session_;
29+
std::shared_ptr<Session> session_;
3030
string getDatabase();
3131
public:
32-
TableSession(Session* session) {
32+
TableSession(std::shared_ptr<Session> session) {
3333
this->session_ = session;
3434
}
35-
~TableSession() {
36-
if (session_) {
37-
delete session_;
38-
session_ = nullptr;
39-
}
40-
}
35+
~TableSession() {}
36+
4137
void insert(Tablet& tablet, bool sorted = false);
4238
void executeNonQueryStatement(const std::string& sql);
4339
unique_ptr<SessionDataSet> executeQueryStatement(const std::string& sql);

iotdb-client/client-cpp/src/main/TableSessionBuilder.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ class TableSessionBuilder : public AbstractSessionBuilder {
6969
AbstractSessionBuilder::nodeUrls = nodeUrls;
7070
return this;
7171
}
72-
TableSession* build() {
72+
std::shared_ptr<TableSession> build() {
7373
sqlDialect = "table";
74-
Session* newSession = new Session(this);
74+
auto newSession = std::make_shared<Session>(this);
7575
newSession->open(false);
76-
return new TableSession(newSession);
76+
return std::make_shared<TableSession>(newSession);
7777
}
7878
};
7979

0 commit comments

Comments
 (0)