Added uiEventFree(). Now to add it to the tests and see what AddressSanitizer says afterward.

This commit is contained in:
Pietro Gagliardi 2019-06-02 10:06:50 -04:00
parent ece22bdddc
commit 9f2796ebac
4 changed files with 38 additions and 1 deletions

View File

@ -24,6 +24,7 @@ static int handlerCmp(const void *a, const void *b)
struct uiEvent {
uiEventOptions opts;
bool internal;
uiprivArray handlers;
uiprivArray unusedIDs;
bool firing;
@ -59,6 +60,26 @@ uiEvent *uiNewEvent(const uiEventOptions *options)
return ret; \
}
void uiFreeEvent(uiEvent *e)
{
if (!uiprivCheckInitializedAndThread())
return;
checkEventNonnull(e, /* nothing */);
if (e->internal) {
uiprivProgrammerErrorFreeingInternalEvent();
return;
}
checkEventNotFiring(e, /* nothing */);
if (e->handlers.len != 0) {
uiprivProgrammerErrorFreeingEventInUse();
return;
}
uiprivArrayFree(e->unusedIDs);
uiprivArrayFree(e->handlers);
uiprivFree(e);
}
static bool checkEventSender(const uiEvent *e, void *sender, const char *func)
{
if (e->opts.Global && sender != NULL) {

View File

@ -36,3 +36,9 @@
#define uiprivProgrammerErrorRecursiveEventFire() \
uiprivProgrammerError("attempt to fire a uiEvent while it is already being fired")
#define uiprivProgrammerErrorFreeingInternalEvent() \
uiprivProgrammerError("attempt to free a libui-provided uiEvent")
#define uiprivProgrammerErrorFreeingEventInUse() \
uiprivProgrammerError("attempt to free a uiEvent that still has handlers registered")

View File

@ -50,6 +50,16 @@ uiEvent *uiNewEvent(const uiEventOptions *options);
It is a programmer error to specify `NULL` for `options`.
### `uiFreeEvent()`
```c
uiprivExtern void uiFreeEvent(uiEvent *e);
```
Frees the given event. The event must not be a libui-provided event, and must not have any handlers registered to it.
It is a programmer error to pass `NULL` for the given event, or to pass in an event that is currently firing.
### `uiEventAddHandler()`
```c

2
ui.h
View File

@ -54,7 +54,7 @@ struct uiEventOptions {
};
uiprivExtern uiEvent *uiNewEvent(const uiEventOptions *options);
// TODO uiFreeEvent()
uiprivExtern void uiFreeEvent(uiEvent *e);
uiprivExtern int uiEventAddHandler(uiEvent *e, uiEventHandler handler, void *sender, void *data);
uiprivExtern void uiEventDeleteHandler(uiEvent *e, int id);
uiprivExtern void uiEventFire(uiEvent *e, void *sender, void *args);