Skip to content

Commit 5a7e2c0

Browse files
committed
fix(worker): worker thread memory access violation
Move the destructor code to the worker thread
1 parent 366cd3f commit 5a7e2c0

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

src/worker.c

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ LCUI_BOOL LCUIWorker_RunTask(LCUI_Worker worker)
8989
return FALSE;
9090
}
9191

92+
static void OnDeleteTask(void *arg)
93+
{
94+
LCUITask_Destroy(arg);
95+
free(arg);
96+
}
97+
98+
static void LCUIWorker_ExecDestroy(LCUI_Worker worker)
99+
{
100+
LinkedList_Clear(&worker->tasks, OnDeleteTask);
101+
LCUIMutex_Unlock(&worker->mutex);
102+
LCUIMutex_Destroy(&worker->mutex);
103+
LCUICond_Destroy(&worker->cond);
104+
free(worker);
105+
}
106+
92107
static void LCUIWorker_Thread(void *arg)
93108
{
94109
LCUI_Worker worker = arg;
@@ -101,7 +116,7 @@ static void LCUIWorker_Thread(void *arg)
101116
LCUICond_Wait(&worker->cond, &worker->mutex);
102117
}
103118
}
104-
LCUIMutex_Unlock(&worker->mutex);
119+
LCUIWorker_ExecDestroy(worker);
105120
LCUIThread_Exit(NULL);
106121
}
107122

@@ -116,26 +131,19 @@ int LCUIWorker_RunAsync(LCUI_Worker worker)
116131
return 0;
117132
}
118133

119-
static void OnDeleteTask(void *arg)
120-
{
121-
LCUITask_Destroy(arg);
122-
free(arg);
123-
}
124-
125134
void LCUIWorker_Destroy(LCUI_Worker worker)
126135
{
127-
if (worker->thread != 0) {
128-
LOG("[worker] worker %u is stopping...\n", worker->thread);
136+
LCUI_Thread thread = worker->thread;
137+
138+
if (worker->active) {
139+
LOG("[worker] worker %u is stopping...\n", thread);
129140
LCUIMutex_Lock(&worker->mutex);
130141
worker->active = FALSE;
131142
LCUICond_Signal(&worker->cond);
132143
LCUIMutex_Unlock(&worker->mutex);
133-
LCUIThread_Join(worker->thread, NULL);
134-
LOG("[worker] worker %u has stopped\n", worker->thread);
135-
worker->thread = 0;
144+
LCUIThread_Join(thread, NULL);
145+
LOG("[worker] worker %u has stopped\n", thread);
146+
return;
136147
}
137-
LCUIMutex_Destroy(&worker->mutex);
138-
LCUICond_Destroy(&worker->cond);
139-
LinkedList_Clear(&worker->tasks, OnDeleteTask);
140-
free(worker);
148+
LCUIWorker_ExecDestroy(worker);
141149
}

0 commit comments

Comments
 (0)