Skip to content

Commit

Permalink
Merge pull request Tencent#124 from anonymalias/co_free_leak
Browse files Browse the repository at this point in the history
fix co_free memory leak and add co_reset interface
  • Loading branch information
leiffyli authored Sep 2, 2019
2 parents 83d2343 + de2f27c commit 03ba1a4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
36 changes: 36 additions & 0 deletions co_routine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,17 @@ void co_free( stCoRoutine_t *co )
free(co->stack_mem->stack_buffer);
free(co->stack_mem);
}
//walkerdu fix at 2018-01-20
//存在内存泄漏
else
{
if(co->save_buffer)
free(co->save_buffer);

if(co->stack_mem->occupy_co == co)
co->stack_mem->occupy_co = NULL;
}

free( co );
}
void co_release( stCoRoutine_t *co )
Expand All @@ -558,6 +569,31 @@ void co_resume( stCoRoutine_t *co )


}


// walkerdu 2018-01-14
// 用于reset超时无法重复使用的协程
void co_reset(stCoRoutine_t * co)
{
if(!co->cStart || co->cIsMain)
return;

co->cStart = 0;
co->cEnd = 0;

// 如果当前协程有共享栈被切出的buff,要进行释放
if(co->save_buffer)
{
free(co->save_buffer);
co->save_buffer = NULL;
co->save_size = 0;
}

// 如果共享栈被当前协程占用,要释放占用标志,否则被切换,会执行save_stack_buffer()
if(co->stack_mem->occupy_co == co)
co->stack_mem->occupy_co = NULL;
}

void co_yield_env( stCoRoutineEnv_t *env )
{

Expand Down
1 change: 1 addition & 0 deletions co_routine.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void co_resume( stCoRoutine_t *co );
void co_yield( stCoRoutine_t *co );
void co_yield_ct(); //ct = current thread
void co_release( stCoRoutine_t *co );
void co_reset(stCoRoutine_t * co);

stCoRoutine_t *co_self();

Expand Down

0 comments on commit 03ba1a4

Please sign in to comment.