Ensure rpmFilter is resized before the updateTask is created. #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This commit addresses a critical issue in the initialization order within the begin() method of the MT6701 class. Previously, the asynchronous update task created by xTaskCreatePinnedToCore could potentially attempt to access the rpmFilter vector before it had been properly initialized and resized. This scenario could lead to undefined behavior, including crashes due to accessing uninitialized memory.
Changes made:
The call to rpmFilter.resize(rpmFilterSize); has been moved to occur before the creation of the update task with xTaskCreatePinnedToCore. This ensures that by the time the update task runs, rpmFilter is already appropriately sized and ready for use. This change prevents a race condition where the update task might execute updateRPMFilter(float newRPM) or any other method relying on rpmFilter, encountering a vector that has not yet been resized. Such a condition was observed to cause a "Guru Meditation Error: Core 1 panic'ed (StoreProhibited)" due to attempts to access or modify the vector before its initialization. Additional inline documentation has been added to clarify the importance of the initialization order for future maintenance and readability. Rationale:
Ensuring the proper initialization sequence in multi-threaded applications is crucial for stability and reliability, especially on platforms like ESP32 where tasks may preempt each other. By resizing rpmFilter before the update task starts, we eliminate the risk of accessing an uninitialized or incorrectly sized vector, thus enhancing the robustness of the encoder's RPM filtering functionality.
This fix is a preventative measure against potential undefined behavior, ensuring that all components are correctly initialized in a deterministic order, which is especially important in a real-time operating system environment like FreeRTOS.