mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-10 05:27:01 +03:00
fixing memory leaks
This commit is contained in:
@@ -18,7 +18,6 @@ namespace bell
|
|||||||
public:
|
public:
|
||||||
std::string taskName;
|
std::string taskName;
|
||||||
int stackSize, core;
|
int stackSize, core;
|
||||||
bool isRunning = false;
|
|
||||||
bool runOnPSRAM;
|
bool runOnPSRAM;
|
||||||
Task(std::string taskName, int stackSize, int priority, int core, bool runOnPSRAM = true)
|
Task(std::string taskName, int stackSize, int priority, int core, bool runOnPSRAM = true)
|
||||||
{
|
{
|
||||||
@@ -27,21 +26,28 @@ namespace bell
|
|||||||
this->core = core;
|
this->core = core;
|
||||||
this->runOnPSRAM = runOnPSRAM;
|
this->runOnPSRAM = runOnPSRAM;
|
||||||
#ifdef ESP_PLATFORM
|
#ifdef ESP_PLATFORM
|
||||||
|
this->xStack = NULL;
|
||||||
this->priority = CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT + priority;
|
this->priority = CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT + priority;
|
||||||
if (this->priority < 0) this->priority = ESP_TASK_PRIO_MIN;
|
if (this->priority < 0) this->priority = ESP_TASK_PRIO_MIN;
|
||||||
|
if (runOnPSRAM) {
|
||||||
|
this->xStack = (StackType_t*) heap_caps_malloc(this->stackSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
virtual ~Task() {
|
||||||
|
#ifdef ESP_PLATFORM
|
||||||
|
if (xStack) heap_caps_free(xStack);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
virtual ~Task() {}
|
|
||||||
|
|
||||||
bool startTask()
|
bool startTask()
|
||||||
{
|
{
|
||||||
#ifdef ESP_PLATFORM
|
#ifdef ESP_PLATFORM
|
||||||
if (runOnPSRAM)
|
if (runOnPSRAM)
|
||||||
{
|
{
|
||||||
xTaskBuffer = (StaticTask_t *)heap_caps_malloc(sizeof(StaticTask_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
xTaskBuffer = (StaticTask_t*) heap_caps_malloc(sizeof(StaticTask_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||||
xStack = (StackType_t *)heap_caps_malloc(this->stackSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
return (xTaskCreateStaticPinnedToCore(taskEntryFuncPSRAM, this->taskName.c_str(), this->stackSize, this,
|
||||||
|
this->priority, xStack, xTaskBuffer, this->core) != NULL);
|
||||||
return (xTaskCreateStaticPinnedToCore(taskEntryFuncPSRAM, this->taskName.c_str(), this->stackSize, this, this->priority, xStack, xTaskBuffer, this->core) != NULL);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -54,64 +60,43 @@ namespace bell
|
|||||||
esp_pthread_set_cfg(&cfg);
|
esp_pthread_set_cfg(&cfg);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return (pthread_create(&_thread, NULL, taskEntryFunc, this) == 0);
|
return (pthread_create(&thread, NULL, taskEntryFunc, this) == 0);
|
||||||
}
|
|
||||||
void waitForTaskToReturn()
|
|
||||||
{
|
|
||||||
#ifdef ESP_PLATFORM
|
|
||||||
if (runOnPSRAM)
|
|
||||||
{
|
|
||||||
while (isRunning)
|
|
||||||
{
|
|
||||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
||||||
}
|
|
||||||
heap_caps_free(xStack);
|
|
||||||
// TCB are cleanup in IDLE task, so give it some time
|
|
||||||
TimerHandle_t timer = xTimerCreate( "cleanup", pdMS_TO_TICKS(5000), pdFALSE, xTaskBuffer,
|
|
||||||
[](TimerHandle_t xTimer) {
|
|
||||||
heap_caps_free(pvTimerGetTimerID(xTimer));
|
|
||||||
xTimerDelete(xTimer, portMAX_DELAY);
|
|
||||||
} );
|
|
||||||
xTimerStart(timer, portMAX_DELAY);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
(void)pthread_join(_thread, NULL);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
(void)pthread_join(_thread, NULL);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void runTask() = 0;
|
virtual void runTask() = 0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
pthread_t thread;
|
||||||
#ifdef ESP_PLATFORM
|
#ifdef ESP_PLATFORM
|
||||||
int priority;
|
int priority;
|
||||||
StaticTask_t *xTaskBuffer;
|
StaticTask_t *xTaskBuffer;
|
||||||
StackType_t *xStack;
|
StackType_t *xStack;
|
||||||
#endif
|
|
||||||
static void *taskEntryFunc(void *This)
|
|
||||||
{
|
|
||||||
((Task *)This)->runTask();
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
#ifdef ESP_PLATFORM
|
|
||||||
static void taskEntryFuncPSRAM(void *This)
|
static void taskEntryFuncPSRAM(void *This)
|
||||||
{
|
{
|
||||||
|
Task* self = (Task*) This;
|
||||||
((Task *)This)->isRunning = true;
|
|
||||||
|
|
||||||
Task *self = (Task *)This;
|
|
||||||
self->isRunning = true;
|
|
||||||
self->runTask();
|
self->runTask();
|
||||||
self->isRunning = false;
|
|
||||||
|
// TCB are cleanup in IDLE task, so give it some time
|
||||||
|
TimerHandle_t timer = xTimerCreate( "cleanup", pdMS_TO_TICKS(5000), pdFALSE, self->xTaskBuffer,
|
||||||
|
[](TimerHandle_t xTimer) {
|
||||||
|
heap_caps_free(pvTimerGetTimerID(xTimer));
|
||||||
|
xTimerDelete(xTimer, portMAX_DELAY);
|
||||||
|
} );
|
||||||
|
xTimerStart(timer, portMAX_DELAY);
|
||||||
|
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
pthread_t _thread;
|
static void *taskEntryFunc(void *This)
|
||||||
|
{
|
||||||
|
Task* self = (Task*) This;
|
||||||
|
self->runTask();
|
||||||
|
pthread_join(self->thread, NULL);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user