Skip to content

Commit 6e6475b

Browse files
committed
fix(terminal):修复终端交互中执行quit断开会话引起的Session没有释放导致内存增长问题
1 parent db05e11 commit 6e6475b

File tree

8 files changed

+31
-20
lines changed

8 files changed

+31
-20
lines changed

examples/terminal/tcp_rpc/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int main(int argc, char **argv)
5050
auto sp_loop = Loop::New();
5151
SetScopeExitAction([sp_loop] { delete sp_loop; });
5252

53-
Terminal term;
53+
Terminal term(sp_loop);
5454
term.setWelcomeText("Welcome to Terminal TcpRPC demo! \r\n");
5555
TcpRpc rpc(sp_loop, &term);
5656
if (!rpc.initialize(bind_addr)) {

examples/terminal/telnetd/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int main(int argc, char **argv)
5050
auto sp_loop = Loop::New();
5151
SetScopeExitAction([sp_loop] { delete sp_loop; });
5252

53-
Terminal term;
53+
Terminal term(sp_loop);
5454
term.setWelcomeText("Welcome to Terminal Telnet demo! \r\n");
5555
Telnetd telnetd(sp_loop, &term);
5656
if (!telnetd.initialize(bind_addr)) {

modules/main/context_imp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ ContextImp::ContextImp() :
7777
sp_thread_pool_(new eventx::ThreadPool(sp_loop_)),
7878
sp_timer_pool_(new eventx::TimerPool(sp_loop_)),
7979
sp_async_(new eventx::Async(sp_thread_pool_)),
80-
sp_terminal_(new terminal::Terminal),
80+
sp_terminal_(new terminal::Terminal(sp_loop_)),
8181
sp_telnetd_(new terminal::Telnetd(sp_loop_, sp_terminal_)),
8282
sp_tcp_rpc_(new terminal::TcpRpc(sp_loop_, sp_terminal_)),
8383
sp_coroutine_(new coroutine::Scheduler(sp_loop_)),

modules/terminal/impl/terminal.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <tbox/base/log.h>
2525

2626
#include "../connection.h"
27-
#include "session_context.h"
2827
#include "dir_node.h"
2928
#include "func_node.h"
3029

@@ -33,7 +32,8 @@ namespace terminal {
3332

3433
using namespace std;
3534

36-
Terminal::Impl::Impl()
35+
Terminal::Impl::Impl(event::Loop *wp_loop)
36+
: wp_loop_(wp_loop)
3737
{
3838
welcome_text_ = \
3939
"\r\n"
@@ -71,7 +71,7 @@ Terminal::Impl::~Impl()
7171

7272
SessionToken Terminal::Impl::newSession(Connection *wp_conn)
7373
{
74-
auto s = new SessionContext;
74+
auto s = session_ctx_pool_.alloc();
7575
auto t = sessions_.alloc(s);
7676
s->wp_conn = wp_conn;
7777
s->token = t;
@@ -82,7 +82,7 @@ bool Terminal::Impl::deleteSession(const SessionToken &st)
8282
{
8383
auto s = sessions_.free(st);
8484
if (s != nullptr) {
85-
delete s;
85+
session_ctx_pool_.free(s);
8686
return true;
8787
}
8888
return false;

modules/terminal/impl/terminal.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@
2121
#define TBOX_TELNETD_TERMINAL_IMPL_H_20220128
2222

2323
#include <tbox/base/cabinet.hpp>
24+
#include <tbox/base/object_pool.hpp>
25+
#include <tbox/event/forward.h>
2426

2527
#include "../terminal.h"
2628
#include "node.h"
29+
#include "session_context.h"
2730

2831
namespace tbox {
2932
namespace terminal {
3033

31-
struct SessionContext;
32-
3334
class Terminal::Impl {
3435
public:
35-
Impl();
36+
Impl(event::Loop *wp_loop);
3637
~Impl();
3738

3839
public:
@@ -92,6 +93,9 @@ class Terminal::Impl {
9293
bool findNode(const std::string &path, Path &node_path) const;
9394

9495
private:
96+
event::Loop *wp_loop_ = nullptr;
97+
98+
ObjectPool<SessionContext> session_ctx_pool_{1};
9599
cabinet::Cabinet<SessionContext> sessions_;
96100
cabinet::Cabinet<Node> nodes_;
97101
NodeToken root_token_;

modules/terminal/impl/terminal_commands.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <tbox/base/log.h>
2626
#include <tbox/util/split_cmdline.h>
2727
#include <tbox/util/string.h>
28+
#include <tbox/event/loop.h>
2829

2930
#include "session_context.h"
3031
#include "dir_node.h"
@@ -180,23 +181,28 @@ void Terminal::Impl::executeLsCmd(SessionContext *s, const Args &args)
180181
s->wp_conn->send(s->token, ss.str());
181182
}
182183

183-
void Terminal::Impl::executeHistoryCmd(SessionContext *s, const Args &args)
184+
void Terminal::Impl::executeHistoryCmd(SessionContext *s, const Args &)
184185
{
185186
stringstream ss;
186187
for (size_t i = 0; i < s->history.size(); ++i) {
187188
const auto &cmd = s->history.at(i);
188189
ss << setw(2) << i << " " << cmd << "\r\n";
189190
}
190191
s->wp_conn->send(s->token, ss.str());
191-
(void)args;
192192
}
193193

194-
void Terminal::Impl::executeExitCmd(SessionContext *s, const Args &args)
194+
void Terminal::Impl::executeExitCmd(SessionContext *s, const Args &)
195195
{
196196
if (!(s->options & kQuietMode))
197197
s->wp_conn->send(s->token, "Bye!\r\n");
198-
s->wp_conn->endSession(s->token);
199-
(void)args;
198+
199+
wp_loop_->runNext(
200+
[this, s] {
201+
s->wp_conn->endSession(s->token);
202+
deleteSession(s->token);
203+
},
204+
__func__
205+
);
200206
}
201207

202208
void Terminal::Impl::executeTreeCmd(SessionContext *s, const Args &args)
@@ -309,7 +315,7 @@ void Terminal::Impl::executeTreeCmd(SessionContext *s, const Args &args)
309315
s->wp_conn->send(s->token, ss.str());
310316
}
311317

312-
void Terminal::Impl::executePwdCmd(SessionContext *s, const Args &args)
318+
void Terminal::Impl::executePwdCmd(SessionContext *s, const Args &)
313319
{
314320
stringstream ss;
315321
ss << '/';
@@ -320,7 +326,6 @@ void Terminal::Impl::executePwdCmd(SessionContext *s, const Args &args)
320326
}
321327
ss << "\r\n";
322328
s->wp_conn->send(s->token, ss.str());
323-
(void)args;
324329
}
325330

326331
bool Terminal::Impl::executeRunHistoryCmd(SessionContext *s, const Args &args)

modules/terminal/terminal.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
namespace tbox {
2626
namespace terminal {
2727

28-
Terminal::Terminal() :
29-
impl_(new Impl)
28+
Terminal::Terminal(event::Loop *wp_loop) :
29+
impl_(new Impl(wp_loop))
3030
{
3131
TBOX_ASSERT(impl_ != nullptr);
3232
}

modules/terminal/terminal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
#include "terminal_interact.h"
2424
#include "terminal_nodes.h"
2525

26+
#include <tbox/event/forward.h>
27+
2628
namespace tbox {
2729
namespace terminal {
2830

2931
class Terminal : public TerminalInteract,
3032
public TerminalNodes {
3133
public:
32-
Terminal();
34+
Terminal(event::Loop *wp_loop);
3335
virtual ~Terminal() override;
3436

3537
public:

0 commit comments

Comments
 (0)