Fix memory leak in lis2dw_monitor_face settings array
state->settings was malloc'd in lis2dw_monitor_face_setup() outside the guard that only runs on first allocation of the face's context. setup() is called again every time the watch wakes from deep sleep (movement.c's app_setup() re-runs watch_faces[i].setup() for every face on wake), so each wake cycle allocated a new settings array and abandoned the previous pointer with no matching free. Since NUM_SETTINGS is a compile-time constant, make settings a fixed-size array embedded directly in lis2dw_monitor_state_t instead of a separately malloc'd pointer, so it's allocated exactly once along with the rest of the state and never leaks.
This commit is contained in:
@@ -31,9 +31,6 @@
|
|||||||
/* Display frequency */
|
/* Display frequency */
|
||||||
#define DISPLAY_FREQUENCY 8
|
#define DISPLAY_FREQUENCY 8
|
||||||
|
|
||||||
/* Settings */
|
|
||||||
#define NUM_SETTINGS 7
|
|
||||||
|
|
||||||
static void _settings_title_display(lis2dw_monitor_state_t *state, char *buf1, char *buf2)
|
static void _settings_title_display(lis2dw_monitor_state_t *state, char *buf1, char *buf2)
|
||||||
{
|
{
|
||||||
char buf[10];
|
char buf[10];
|
||||||
@@ -541,7 +538,6 @@ void lis2dw_monitor_face_setup(uint8_t watch_face_index, void **context_ptr)
|
|||||||
|
|
||||||
/* Initialize settings */
|
/* Initialize settings */
|
||||||
uint8_t settings_page = 0;
|
uint8_t settings_page = 0;
|
||||||
state->settings = malloc(NUM_SETTINGS * sizeof(lis2dw_settings_t));
|
|
||||||
state->settings[settings_page].display = _settings_mode_display;
|
state->settings[settings_page].display = _settings_mode_display;
|
||||||
state->settings[settings_page].advance = _settings_mode_advance;
|
state->settings[settings_page].advance = _settings_mode_advance;
|
||||||
settings_page++;
|
settings_page++;
|
||||||
|
|||||||
@@ -56,13 +56,16 @@ typedef struct {
|
|||||||
void (*advance)(void *);
|
void (*advance)(void *);
|
||||||
} lis2dw_settings_t;
|
} lis2dw_settings_t;
|
||||||
|
|
||||||
|
/* Number of settings pages */
|
||||||
|
#define NUM_SETTINGS 7
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t axis:2; /* Axis to display */
|
uint8_t axis:2; /* Axis to display */
|
||||||
lis2dw_reading_t reading; /* Current reading */
|
lis2dw_reading_t reading; /* Current reading */
|
||||||
lis2dw_monitor_page_t page; /* Displayed page */
|
lis2dw_monitor_page_t page; /* Displayed page */
|
||||||
lis2dw_device_state_t ds; /* Device state */
|
lis2dw_device_state_t ds; /* Device state */
|
||||||
uint8_t settings_page:3; /* Subpage in settings */
|
uint8_t settings_page:3; /* Subpage in settings */
|
||||||
lis2dw_settings_t *settings; /* Settings config */
|
lis2dw_settings_t settings[NUM_SETTINGS]; /* Settings config */
|
||||||
uint8_t show_title:6; /* Display face title */
|
uint8_t show_title:6; /* Display face title */
|
||||||
} lis2dw_monitor_state_t;
|
} lis2dw_monitor_state_t;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user