Raspberry Pi5 Project

What do you want to achieve?

I currently have 2 screens updating, one screen has 3 values that are updating dynamically and the 2nd screen after having 2 detections lv_label_set_text_fmt those values into 1 out of 10 of the lvgl widgets then has 2 more senses and inputs that value into the next. I am trying to add 2 more screens that will be updating with 6 values on the 1st screen and 20 values onto the next screen as explained before.

What have you tried so far?

I have tried to update and believe I am having corruption in memory as the total score goes from 9 to 17658948765 and current hole says a crazy number as well when that’s not even possible with the sensor values being 4.

Screenshot or video

Others

  • SquareLine Studio version: latest
  • Operating system: Raspberry PI OS 12 Bookworm
  • **Target hardware:**Raspberry PI 5

Hi,

The user is encountering memory corruption (e.g., scores turning into garbage values like -1110238145) and a segmentation fault in their Raspberry Pi 5 project using SquareLine Studio. These issues are likely caused by:

  1. Memory Management Errors

    • Invalid pointer usage (e.g., accessing freed memory).
    • Widgets/labels being created or freed incorrectly.
  2. Format Specifier Mismatches

    • Using %d for non-integer variables (or vice versa) in lv_label_set_text_fmt().
  3. Uninitialized Variables

    • Variables like score or current_hole might not be initialized, leading to garbage values.
  4. Thread Safety Issues

    • Asynchronous updates to UI elements without proper synchronization (e.g., mutexes).

Solution Steps

1. Verify Format Specifiers

Ensure the format specifiers in lv_label_set_text_fmt() match the variable types. For example:

int score = 0;  
lv_label_set_text_fmt(label_score, "Score: %d", score);  // Use %d for integers  

If using float, use %f:

float sensor_value = 4.5;  
lv_label_set_text_fmt(label_sensor, "Value: %.2f", sensor_value);  

2. Initialize Variables

Always initialize variables to avoid garbage values:

int current_hole = 1;  // Start at 1, not 0  
int score = 0;  

3. Check Widget Lifecycle

  • Ensure widgets are created once and not recreated unnecessarily.
  • Avoid freeing labels/widgets that are still in use.

4. Use Debugging Tools

Run the program with valgrind to detect memory leaks or invalid accesses:

valgrind ./your_program  

Look for errors like “Invalid read/write” or “Use of uninitialized value.”

5. Add Debug Logs

Print variable values before updating the UI to catch inconsistencies:

printf("DEBUG: Score=%d, Hole=%d\n", score, current_hole);  // Check if values make sense  
lv_label_set_text_fmt(label_score, "Score: %d", score);  

6. Review Thread Safety

If using multiple threads to update the UI:

  • Use mutexes to protect shared resources (e.g., labels).
  • Avoid updating UI elements from interrupts or callbacks without synchronization.

7. Check Sensor Inputs

Ensure sensor values are within expected ranges before using them:

if (sensor_value < 0 || sensor_value > 10) {  
    printf("ERROR: Invalid sensor value: %d\n", sensor_value);  
    return;  
}  

Example Fix

// Initialize variables  
int score = 0;  
int current_hole = 1;  

// Update labels safely  
void update_score_label() {  
    printf("DEBUG: Updating score=%d\n", score);  // Debug log  
    lv_label_set_text_fmt(label_score, "Score: %d", score);  // Correct format  
}  

// Handle sensor input  
void on_sensor_detection(int sensor_value) {  
    if (sensor_value < 0 || sensor_value > 4) {  // Validate input  
        printf("ERROR: Invalid sensor value: %d\n", sensor_value);  
        return;  
    }  
    score += sensor_value;  
    update_score_label();  
}  

Final Notes

  • If the issue persists, share the relevant code snippets (e.g., label updates, sensor handling) for further analysis.
  • Simplify the project temporarily (e.g., test with 1 screen) to isolate the problem.