fix uieventhandler's crash(caused by runnable)

This commit is contained in:
侯歌 2021-08-05 17:36:57 +08:00
parent c09ccdec87
commit c42bc74405
3 changed files with 18 additions and 6 deletions

View File

@ -16,6 +16,10 @@ public:
Runnable(){
run=nullptr;
}
Runnable(const Runnable&b){
run=b.run;
ID=b.ID;
}
Runnable(const std::function<void()>&f){
run=f;
}

View File

@ -29,10 +29,9 @@ int UIEventSource::handleEvents(){
if(GraphDevice::getInstance().needCompose())
GraphDevice::getInstance().ComposeSurfaces();
if(hasDelayedRunners()){
size_t old=mRunnables.size();
RUNNER runner=mRunnables.front();
runner.run();
mRunnables.erase(mRunnables.begin());
mRunnables.pop_front();
}
return 0;
}
@ -40,7 +39,7 @@ int UIEventSource::handleEvents(){
void UIEventSource::post(Runnable& run,DWORD delayedtime){
RUNNER runner;
runner.time=SystemClock::uptimeMillis()+delayedtime;
runner.run.ID=mID++;
run.ID=mID++;
runner.run=run;
for(auto itr=mRunnables.begin();itr!=mRunnables.end();itr++){
if(runner.time<itr->time){

View File

@ -5,6 +5,8 @@
#include <core/systemclock.h>
#include <cdlog.h>
#include <functional>
#include <core/uieventsource.h>
class LOOPER:public testing::Test{
public :
@ -116,7 +118,14 @@ TEST_F(LOOPER,eventhandler){
}
TEST_F(LOOPER,loop){
Looper loop(false);
loop.pollAll(1000);
loop.pollAll(1000);
loop.pollAll(1000);
UIEventSource*handler=new UIEventSource(nullptr);
loop.addEventHandler(handler);
Runnable run;
int count=0;
run=[&](){
printf("count=%d\r\n",count++);
handler->post(run,count++);
};
handler->post(run,10);
while(1)loop.pollAll(100);
}