Skip to content

Commit 76b4f09

Browse files
author
Jay Gu
committed
Merge pull request #131 from ylow/master
Fix for a memory leak when creating empty sframe or sarray
2 parents 5a33e3f + a77aaf2 commit 76b4f09

File tree

4 files changed

+40
-19
lines changed

4 files changed

+40
-19
lines changed

oss_src/sframe/sarray.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ class sarray : public swriter_base<swriter_impl::output_iterator<T> > {
587587
delete writer;
588588
writing = false;
589589

590-
if (size() > 0) keep_array_file_ref();
590+
keep_array_file_ref();
591591
}
592592

593593
/**

oss_src/sframe/sframe.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,12 @@ void sframe::close() {
630630
} else {
631631
index_info.nrows = 0;
632632
}
633+
634+
if (!group_index.group_index_file.empty()) {
635+
index_file_handle.push_back(
636+
fileio::file_handle_pool::get_instance().register_file(
637+
group_index.group_index_file));
638+
}
633639
group_writer.reset();
634640
write_sframe_index_file(index_file, index_info);
635641
inited = true;
@@ -640,7 +646,7 @@ void sframe::close() {
640646
columns[i]->open_for_read(group_index.columns[i]);
641647
}
642648
// we can now read.
643-
if (index_info.nrows > 0) keep_array_file_ref();
649+
keep_array_file_ref();
644650
}
645651

646652

oss_src/unity/lib/unity_sarray.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ namespace graphlab {
4848

4949
using namespace query_eval;
5050

51+
static std::shared_ptr<sarray<flexible_type>> get_empty_sarray() {
52+
// make empty sarray and keep it around, reusing it whenever
53+
// I need an empty sarray . We are intentionally leaking this object.
54+
// Otherwise the termination of this will race against the cleanup of the
55+
// cache files.
56+
static std::shared_ptr<sarray<flexible_type> >* empty_sarray = nullptr;
57+
static graphlab::mutex static_sa_lock;
58+
std::lock_guard<graphlab::mutex> guard(static_sa_lock);
59+
if (empty_sarray == nullptr) {
60+
empty_sarray = new std::shared_ptr<sarray<flexible_type>>();
61+
(*empty_sarray) = std::make_shared<sarray<flexible_type>>();
62+
(*empty_sarray)->open_for_write(1);
63+
(*empty_sarray)->set_type(flex_type_enum::FLOAT);
64+
(*empty_sarray)->close();
65+
}
66+
return *empty_sarray;
67+
}
68+
5169
unity_sarray::unity_sarray() {
5270
// make empty sarray and keep it around, reusing it whenever
5371
// I need an empty sarray
@@ -288,17 +306,8 @@ void unity_sarray::save_array_by_index_file(std::string index_file) {
288306
}
289307

290308
void unity_sarray::clear() {
291-
static std::shared_ptr<sarray<flexible_type> > empty_sarray;
292-
static graphlab::mutex static_sa_lock;
293-
std::lock_guard<graphlab::mutex> guard(static_sa_lock);
294-
if (empty_sarray == nullptr) {
295-
empty_sarray = std::make_shared<sarray<flexible_type>>();
296-
empty_sarray->open_for_write(1);
297-
empty_sarray->set_type(flex_type_enum::FLOAT);
298-
empty_sarray->close();
299-
}
300309
m_planner_node =
301-
query_eval::op_sarray_source::make_planner_node(empty_sarray);
310+
query_eval::op_sarray_source::make_planner_node(get_empty_sarray());
302311
}
303312

304313
void unity_sarray::save(oarchive& oarc) const {

oss_src/unity/lib/unity_sframe.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,24 @@ namespace graphlab {
3737

3838
using namespace graphlab::query_eval;
3939

40-
unity_sframe::unity_sframe() {
40+
static std::shared_ptr<sframe> get_empty_sframe() {
4141
// make empty sframe and keep it around, reusing it whenever
42-
// I need an empty sframe
43-
static std::shared_ptr<sframe> sf;
42+
// I need an empty sframe. We are intentionally leaking this object.
43+
// Otherwise the termination of this will race against the cleanup of the
44+
// cache files.
45+
static std::shared_ptr<sframe>* sf = nullptr;
4446
static graphlab::mutex static_sf_lock;
4547
std::lock_guard<graphlab::mutex> guard(static_sf_lock);
4648
if (sf == nullptr) {
47-
sf = std::make_shared<sframe>();
48-
sf->open_for_write({}, {}, "", 1);
49-
sf->close();
49+
sf = new std::shared_ptr<sframe>();
50+
(*sf) = std::make_shared<sframe>();
51+
(*sf)->open_for_write({}, {}, "", 1);
52+
(*sf)->close();
5053
}
51-
this->set_sframe(sf);
54+
return *sf;
55+
}
56+
unity_sframe::unity_sframe() {
57+
this->set_sframe(get_empty_sframe());
5258
}
5359

5460
unity_sframe::~unity_sframe() { clear(); }

0 commit comments

Comments
 (0)