[input] Plug a pile of memory leaks

While most were "harmless" in that they were held pointers at shutdown,
a few bugs were exposed in the imt code.
This commit is contained in:
Bill Currie 2022-05-12 19:01:45 +09:00
parent 973ae0ad54
commit 8db8f91e7d
10 changed files with 75 additions and 3 deletions

View file

@ -365,6 +365,7 @@ struct IE_event_s;
int IN_Binding_HandleEvent (const struct IE_event_s *ie_event);
void IN_Binding_Activate (void);
void IN_Binding_Init (void);
void IN_Binding_Shutdown (void);
struct plitem_s;
void IN_Binding_SaveConfig (struct plitem_s *config);
void IN_Binding_LoadConfig (struct plitem_s *config);

View file

@ -130,6 +130,7 @@ void IMT_BindButton (imt_t *imt, int button, const char *binding);
qboolean IMT_ProcessAxis (int axis, int value);
qboolean IMT_ProcessButton (int button, int state);
void IMT_Init (void);
void IMT_Shutdown (void);
struct plitem_s;
void IMT_SaveConfig (struct plitem_s *config);
void IMT_SaveAxisConfig (struct plitem_s *axes, int axis_ind, int dev_axis);

View file

@ -191,7 +191,6 @@ check_device (const char *path)
{
device_t *dev;
int fd;
dstring_t *buff = dstring_newstr ();
fd = open (path, O_RDWR);
if (fd == -1)
@ -208,9 +207,11 @@ check_device (const char *path)
dev->path = strdup (path);
dev->fd = fd;
dstring_t *buff = dstring_newstr ();
dev->name = strdup (get_string (fd, EVIOCGNAME, buff));
dev->phys = strdup (get_string (fd, EVIOCGPHYS, buff));
dev->uniq = strdup (get_string (fd, EVIOCGUNIQ, buff));
dstring_delete (buff);
setup_buttons(dev);
setup_axes(dev);

View file

@ -93,9 +93,15 @@ IN_AxisRemoveListener (in_axis_t *axis, axis_listener_t listener, void *data)
}
}
static void
in_axis_shutdown (void *data)
{
Hash_DelTable (axis_tab);
}
static void __attribute__((constructor))
in_axis_init (void)
{
axis_tab = Hash_NewTable (127, axis_get_key, axis_free, 0, 0);
Sys_RegisterShutdown (in_axis_shutdown, 0);
}

View file

@ -788,6 +788,19 @@ IN_Binding_Init (void)
}
}
void
IN_Binding_Shutdown ()
{
for (in_devbindings_t *db = devbindings_list; db; db = db->next) {
clear_connection (db);
}
for (unsigned i = 0; i < devbindings._size; i++) {
free (devbindings._map[i]);
}
free (devbindings._map);
DARRAY_CLEAR (&known_devices);
}
void
IN_Binding_SaveConfig (plitem_t *config)
{

View file

@ -62,7 +62,12 @@ button_get_key (const void *b, void *data)
static void
button_free (void *b, void *data)
{
free (b);
regbutton_t *rb = b;
if (rb->button->listeners) {
DARRAY_CLEAR (rb->button->listeners);
free (rb->button->listeners);
}
free (rb);
}
static void
@ -222,8 +227,15 @@ IN_ButtonRemoveListener (in_button_t *button, button_listener_t listener,
}
}
static void
in_button_shutdown (void *data)
{
Hash_DelTable (button_tab);
}
static void __attribute__((constructor))
in_button_init (void)
{
button_tab = Hash_NewTable (127, button_get_key, button_free, 0, 0);
Sys_RegisterShutdown (in_button_shutdown, 0);
}

View file

@ -539,6 +539,11 @@ IN_shutdown (void *data)
rd->driver.shutdown (rd->data);
}
}
IN_Binding_Shutdown ();
IMT_Shutdown ();
DARRAY_CLEAR (&in_drivers);
DARRAY_CLEAR (&in_devices);
}
void

View file

@ -76,6 +76,11 @@ static void
in_evdev_shutdown (void *data)
{
inputlib_close ();
for (unsigned i = 0; i < devmap._size; i++) {
free (devmap._map[i]);
}
free (devmap._map);
}
static void

View file

@ -120,3 +120,16 @@ IE_Get_Focus (void)
{
return focus;
}
static void
in_event_shutdown (void *data)
{
DARRAY_CLEAR (&ie_handlers);
}
static void __attribute__((constructor))
in_event_init (void)
{
//FIXME see in_evdev
Sys_RegisterShutdown (in_event_shutdown, 0);
}

View file

@ -835,6 +835,8 @@ imt_drop_all_f (void)
for (size_t i = 0; i < imt->button_bindings.size; i++) {
free_button_binding (imt->button_bindings.a[i]);
}
DARRAY_CLEAR (&imt->axis_bindings);
DARRAY_CLEAR (&imt->button_bindings);
free ((char *) imt->name);
free (imt);
}
@ -940,6 +942,19 @@ IMT_Init (void)
}
}
void
IMT_Shutdown (void)
{
imt_drop_all_f ();
Hash_DelTable (recipe_tab);
delete_memsuper (binding_mem);
binding_mem = 0;
DARRAY_CLEAR (&axis_blocks);
DARRAY_CLEAR (&button_blocks);
DARRAY_CLEAR (&in_contexts);
}
void
IMT_SaveConfig (plitem_t *config)
{
@ -1078,8 +1093,8 @@ IMT_SaveButtonConfig (plitem_t *buttons, int button_ind, int dev_button)
void
IMT_LoadConfig (plitem_t *config)
{
imt_reset_blocks ();
imt_drop_all_f ();
imt_reset_blocks ();
plitem_t *ctx_list = PL_ObjectForKey (config, "contexts");
if (PL_Type (ctx_list) != QFArray) {