关闭标记器后,您需要某种释放ItemType
s的方法。如果您之前释放它们,标记化将读入释放的内存,这是您非常不想做的事情。
我猜你的框架应该提供某种方式来释放任何输入HM_Put
. 如果没有,您需要自己执行此操作。
例如(这可以通过决定在令牌和to_lower
ed 令牌之间保留哪个来优化):
typedef struct t_tofree {
struct t_tofree *next;
ItemType *item;
char *token; // Maybe superfluous if item->data points here...
};
t_tofree *toFree = NULL;
void mustFree(ItemType *item, char *token) {
t_tofree *new = malloc(sizeof(t_tofree));
new->next = toFree;
new->item = item;
new->token = token;
toFree = new;
}
void freeAll() {
while (toFree) {
t_toFree *next = toFree->next;
free(toFree->token); toFree->token = NULL;
// The line below if token is *not* data and both were allocated.
free(toFree->item->data; toFree->item->data = NULL;
// Other cleanup on item?
free(toFree->item); toFree->item = NULL;
free(toFree); toFree = next;
}
}
...
for(next_token = TKGetNextToken(tokenizer); next_token != NULL; next_token = TKGetNextToken(tokenizer))
{
ItemType* item = malloc(sizeof(ItemType));
item->data = to_lower(next_token);
item->fileName = filename;
item->occ = 1;
HM_Put(hm, item);
// Probably you can free next_token here, and only store item->data
mustFree(item, next_token);
}
...
// Here you're REALLY REALLY sure you won't use tokens or items
// (it's agreed that TKGetNextToken returns alloc'ed memory)
freeAll();