Skip to content

Commit 580a446

Browse files
committed
1. fix co_free memory leak: issue:71
2. add co_reset interface: issue:72
1 parent 8ce6dfe commit 580a446

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

co_routine.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,14 @@ void co_free( stCoRoutine_t *co )
535535
free(co->stack_mem->stack_buffer);
536536
free(co->stack_mem);
537537
}
538+
//walkerdu fix at 2018-01-20
539+
//存在内存泄漏
540+
else
541+
{
542+
if(co->save_buffer)
543+
free(co->save_buffer);
544+
}
545+
538546
free( co );
539547
}
540548
void co_release( stCoRoutine_t *co )
@@ -558,6 +566,32 @@ void co_resume( stCoRoutine_t *co )
558566

559567

560568
}
569+
570+
571+
// walkerdu 2018-01-14
572+
// 用于reset超时无法重复使用的协程
573+
void co_reset(stCoRoutine_t * co)
574+
{
575+
if(!co->cStart || co->cIsMain)
576+
return;
577+
578+
co->cStart = 0;
579+
co->cEnd = 0;
580+
581+
// 如果当前协程有共享栈被切出的buff,要进行释放
582+
if(co->save_buffer)
583+
{
584+
free(co->save_buffer);
585+
co->save_buffer = NULL;
586+
co->save_size = 0;
587+
}
588+
589+
// 如果共享栈被当前协程占用,要释放占用标志,否则被切换,会执行save_stack_buffer()
590+
if(co->stack_mem->occupy_co == co)
591+
co->stack_mem->occupy_co = NULL;
592+
}
593+
594+
561595
void co_yield_env( stCoRoutineEnv_t *env )
562596
{
563597

co_routine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void co_resume( stCoRoutine_t *co );
5050
void co_yield( stCoRoutine_t *co );
5151
void co_yield_ct(); //ct = current thread
5252
void co_release( stCoRoutine_t *co );
53+
void co_reset(stCoRoutine_t * co);
5354

5455
stCoRoutine_t *co_self();
5556

0 commit comments

Comments
 (0)