Permalink
Browse files

load: use AssocData to free load handles

Instead of an explicit call in jim.c, store load
handles as AssocData so that it is automatically cleaned up.

Signed-off-by: Steve Bennett <steveb@workware.net.au>
  • Loading branch information...
1 parent 9c1e2d9 commit 73a0367ef52e3c9651b8920d06ebbbe526c96c60 @msteveb committed Jan 3, 2014
Showing with 14 additions and 11 deletions.
  1. +14 −8 jim-load.c
  2. +0 −3 jim.c
View
@@ -20,6 +20,8 @@
#define RTLD_LOCAL 0
#endif
+static void JimFreeLoadHandles(Jim_Interp *interp, void *data);
+
/**
* Note that Jim_LoadLibrary() requires a path to an existing file.
*
@@ -66,11 +68,13 @@ int Jim_LoadLibrary(Jim_Interp *interp, const char *pathName)
}
else if (onload(interp) != JIM_ERR) {
/* Add this handle to the stack of handles to be freed */
- if (!interp->loadHandles) {
- interp->loadHandles = Jim_Alloc(sizeof(*interp->loadHandles));
- Jim_InitStack(interp->loadHandles);
+ Jim_Stack *loadHandles = Jim_GetAssocData(interp, "load::handles");
+ if (loadHandles == NULL) {
+ loadHandles = Jim_Alloc(sizeof(*loadHandles));
+ Jim_InitStack(loadHandles);
+ Jim_SetAssocData(interp, "load::handles", JimFreeLoadHandles, loadHandles);
}
- Jim_StackPush(interp->loadHandles, handle);
+ Jim_StackPush(loadHandles, handle);
Jim_SetEmptyResult(interp);
@@ -88,11 +92,13 @@ static void JimFreeOneLoadHandle(void *handle)
dlclose(handle);
}
-void Jim_FreeLoadHandles(Jim_Interp *interp)
+static void JimFreeLoadHandles(Jim_Interp *interp, void *data)
{
- if (interp->loadHandles) {
- Jim_FreeStackElements(interp->loadHandles, JimFreeOneLoadHandle);
- Jim_Free(interp->loadHandles);
+ Jim_Stack *handles = data;
+
+ if (handles) {
+ Jim_FreeStackElements(handles, JimFreeOneLoadHandle);
+ Jim_Free(handles);
}
}
View
3 jim.c
@@ -5484,9 +5484,6 @@ void Jim_FreeInterp(Jim_Interp *i)
Jim_Free(cf);
cf = nextcf;
}
-#ifdef jim_ext_load
- Jim_FreeLoadHandles(i);
-#endif
/* Free the interpreter structure. */
Jim_Free(i);

0 comments on commit 73a0367

Please sign in to comment.