Skip to content

Commit

Permalink
Merge pull request #130 from openledger/thread_d_memory_leak
Browse files Browse the repository at this point in the history
Fix memory leak. Not all tasks are deleted in thread_d dtor.
  • Loading branch information
abitmore authored May 21, 2019
2 parents ab6aa05 + 55ad023 commit 397830b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/thread/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@ namespace fc {
}

thread::~thread() {
if( my )
if( my && is_running() )
{
quit();
}

delete my;
}
Expand Down Expand Up @@ -342,6 +344,10 @@ namespace fc {

void thread::async_task( task_base* t, const priority& p, const time_point& tp ) {
assert(my);
if ( !is_running() )
{
FC_THROW_EXCEPTION( canceled_exception, "Thread is not running.");
}
t->_when = tp;
task_base* stale_head = my->task_in_queue.load(boost::memory_order_relaxed);
do { t->_next = stale_head;
Expand Down
15 changes: 11 additions & 4 deletions src/thread/thread_d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ namespace fc {
current = nullptr;
fc::context* temp;
for (fc::context* ready_context : ready_heap)
delete ready_context;
{
if (ready_context->cur_task)
{
ready_context->cur_task->release();
ready_context->cur_task = nullptr;
}
delete ready_context;
}
ready_heap.clear();
while (blocked)
{
Expand Down Expand Up @@ -524,10 +531,10 @@ namespace fc {

next->_set_active_context( current );
current->cur_task = next;
next->run();
fc::shared_ptr<task_base> next_ptr(next);
next_ptr->run();
current->cur_task = 0;
next->_set_active_context(0);
next->release();
next_ptr->_set_active_context(0);
current->reinitialize();
}

Expand Down

0 comments on commit 397830b

Please sign in to comment.