diff --git a/src/dialog.c b/src/dialog.c index 05087474..7b0b4f28 100644 --- a/src/dialog.c +++ b/src/dialog.c @@ -191,24 +191,38 @@ static WMArray *LoadHistory(const char *filename, int max) WMPropList *plitem; WMArray *history; int i, num; + char *str; history = WMCreateArrayWithDestructor(1, wfree); WMAddToArray(history, wstrdup("")); plhistory = WMReadPropListFromFile(filename); - if (plhistory && WMIsPLArray(plhistory)) { - num = WMGetPropListItemCount(plhistory); + if (plhistory) { + if (WMIsPLArray(plhistory)) { + num = WMGetPropListItemCount(plhistory); - for (i = 0; i < num; ++i) { - plitem = WMGetFromPLArray(plhistory, i); - if (WMIsPLString(plitem) && WMFindInArray(history, strmatch, - WMGetFromPLString(plitem)) == WANotFound) { - WMAddToArray(history, WMGetFromPLString(plitem)); - if (--max <= 0) - break; + for (i = 0; i < num; ++i) { + plitem = WMGetFromPLArray(plhistory, i); + if (WMIsPLString(plitem)) { + str = WMGetFromPLString(plitem); + if (WMFindInArray(history, strmatch, str) == WANotFound) { + /* + * The string here is duplicated because it will be freed + * automatically when the array is deleted. This is not really + * great because it is already an allocated string, + * unfortunately we cannot re-use it because it will be freed + * when we discard the PL (and we don't want to waste the PL's + * memory either) + */ + WMAddToArray(history, wstrdup(str)); + if (--max <= 0) + break; + } + } } } + WMReleasePropList(plhistory); } return history;