Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[15_1] fix memory leakage of tm_ostream #208

Merged
merged 4 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions System/IO/tm_ostream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class buffered_ostream_rep : public tm_ostream_rep {
buffered_ostream_rep::buffered_ostream_rep (tm_ostream_rep* master2)
: master (master2) {}

buffered_ostream_rep::~buffered_ostream_rep () {}
buffered_ostream_rep::~buffered_ostream_rep () { DEC_COUNT (master); }

bool
buffered_ostream_rep::is_writable () const {
Expand Down Expand Up @@ -191,14 +191,16 @@ tm_ostream::flush () {
void
tm_ostream::buffer () {
rep= tm_new<buffered_ostream_rep> (rep);
INC_COUNT (rep);
}

string
tm_ostream::unbuffer () {
buffered_ostream_rep* ptr= (buffered_ostream_rep*) rep;
rep = ptr->master;
string r = ptr->buf;
tm_delete<buffered_ostream_rep> (ptr);
INC_COUNT (rep);
DEC_COUNT (ptr);
return r;
}

Expand Down
1 change: 1 addition & 0 deletions System/IO/tm_ostream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class string;
class tm_ostream;

class tm_ostream_rep {
public:
int ref_count;

public:
Expand Down
17 changes: 11 additions & 6 deletions tests/System/Classes/tm_timer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ test_same (tm_ostream& a, string b) {

string
to_zero (tm_ostream& out) {
string s1 = out.unbuffer ();
char* ans = as_charp (s1);
char* current= ans;
string s1= out.unbuffer ();
c_string ans (s1);
char* current= ans;
while ((current= strstr (current, "took"))) {
current+= strlen ("took");
while (*current && (*current == '(')) {
Expand All @@ -61,7 +61,7 @@ to_zero (tm_ostream& out) {
current++;
}
}
return ans;
return (char*) ans;
}

TEST_MEMORY_LEAK_INIT
Expand Down Expand Up @@ -106,8 +106,6 @@ TEST_CASE ("function texmacs_time") {
CHECK (t2 >= t1);
}

TEST_MEMORY_LEAK_ALL

TEST_CASE ("function bench_start and bench_cumul") {
tm_ostream ostream;
ostream.buffer ();
Expand Down Expand Up @@ -238,3 +236,10 @@ TEST_CASE ("function bench_print") {
CHECK (out == b);
}
}

TEST_CASE ("testing memory leakage of tm_timer") {
bench_reset ("task");
bench_reset ("task1");
bench_reset ("task2");
CHECK_EQ (mem_used (), mem_lolly);
}
4 changes: 4 additions & 0 deletions tests/System/IO/tm_ostream_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ test_same (tm_ostream& a, string b) {
return sa == b;
}

TEST_MEMORY_LEAK_INIT

TEST_CASE ("function is_writable") {
tm_ostream t;
CHECK (t->is_writable () == true);
Expand Down Expand Up @@ -185,3 +187,5 @@ TEST_CASE ("cout/cerr") {
cerr << "Lolly" << LF;
cerr << "棒棒糖" << LF;
}

TEST_MEMORY_LEAK_ALL
Loading