Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GlobalContext destroy order #738

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions src/libAtomVM/globalcontext.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,8 @@ COLD_FUNC void globalcontext_destroy(GlobalContext *glb)
{
sys_free_platform(glb);

#ifndef AVM_NO_SMP
smp_condvar_destroy(glb->schedulers_cv);
smp_mutex_destroy(glb->schedulers_mutex);
smp_rwlock_destroy(glb->modules_lock);
#endif
synclist_destroy(&glb->registered_processes);
synclist_destroy(&glb->processes_table);
// Destroy every remaining refc binaries.
struct ListHead *item;
struct ListHead *tmp;
struct ListHead *refc_binaries = synclist_nolock(&glb->refc_binaries);
MUTABLE_LIST_FOR_EACH (item, tmp, refc_binaries) {
struct RefcBinary *refc = GET_LIST_ENTRY(item, struct RefcBinary, head);
refc_binary_destroy(refc, glb);
}
synclist_destroy(&glb->refc_binaries);

struct ListHead *open_avm_packs = synclist_nolock(&glb->avmpack_data);
MUTABLE_LIST_FOR_EACH (item, tmp, open_avm_packs) {
Expand All @@ -218,6 +204,22 @@ COLD_FUNC void globalcontext_destroy(GlobalContext *glb)
}
synclist_destroy(&glb->listeners);

// Destroy select events before resources
struct ListHead *select_events = synclist_nolock(&glb->select_events);
MUTABLE_LIST_FOR_EACH (item, tmp, select_events) {
struct SelectEvent *select_event = GET_LIST_ENTRY(item, struct SelectEvent, head);
free((void *) select_event);
}
synclist_destroy(&glb->select_events);

// Destroy refc binaries including resources
struct ListHead *refc_binaries = synclist_nolock(&glb->refc_binaries);
MUTABLE_LIST_FOR_EACH (item, tmp, refc_binaries) {
struct RefcBinary *refc = GET_LIST_ENTRY(item, struct RefcBinary, head);
refc_binary_destroy(refc, glb);
}
synclist_destroy(&glb->refc_binaries);

// Destroy resource types
struct ListHead *resource_types = synclist_nolock(&glb->resource_types);
MUTABLE_LIST_FOR_EACH (item, tmp, resource_types) {
Expand All @@ -226,12 +228,13 @@ COLD_FUNC void globalcontext_destroy(GlobalContext *glb)
}
synclist_destroy(&glb->resource_types);

struct ListHead *select_events = synclist_nolock(&glb->select_events);
MUTABLE_LIST_FOR_EACH (item, tmp, select_events) {
struct SelectEvent *select_event = GET_LIST_ENTRY(item, struct SelectEvent, head);
free((void *) select_event);
}
synclist_destroy(&glb->select_events);
#ifndef AVM_NO_SMP
smp_condvar_destroy(glb->schedulers_cv);
smp_mutex_destroy(glb->schedulers_mutex);
smp_rwlock_destroy(glb->modules_lock);
#endif
synclist_destroy(&glb->registered_processes);
synclist_destroy(&glb->processes_table);

free(glb);
}
Expand Down