Skip to content

Commit 87a9e59

Browse files
committed
Remove memory leaks when exiting program
1 parent 3711f97 commit 87a9e59

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

cmd.c

+33
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct DatareqCmd {
4848
static struct HashTable *xdrCommandHash = NULL;
4949
static struct DatareqCmd *xdrDatareqList = NULL;
5050
static struct HashTable *xdrErrorHash = NULL;
51+
static int cleanup_reg = 0;
5152

5253
/// Value in the PROT element in the CMD structure that indicates protected cmd
5354
#define CMD_PROTECTED 1
@@ -108,6 +109,25 @@ static void fakeStatusCommand(int socket, unsigned char cmd, void * data,
108109

109110
static ProcessData *cmdGProc = NULL;
110111

112+
static void CMD_hash_cleanup(void)
113+
{
114+
struct DatareqCmd *node;
115+
cleanup_reg = 0;
116+
117+
if (xdrCommandHash)
118+
HASH_free_table(xdrCommandHash);
119+
xdrCommandHash = NULL;
120+
121+
if (xdrErrorHash)
122+
HASH_free_table(xdrErrorHash);
123+
xdrErrorHash = NULL;
124+
125+
while((node = xdrDatareqList)) {
126+
xdrDatareqList = node->next;
127+
free(node);
128+
}
129+
}
130+
111131
void heartbeat_populator(void *arg, XDR_tx_struct cb, void *cb_args)
112132
{
113133
struct CommandCbArg *cmds = (struct CommandCbArg*)arg;
@@ -1074,6 +1094,10 @@ void CMD_register_command(struct CMD_XDRCommandInfo *cmd, int override)
10741094

10751095
if (cmd->command) {
10761096
if (!xdrCommandHash) {
1097+
if (!cleanup_reg)
1098+
atexit(&CMD_hash_cleanup);
1099+
cleanup_reg = 1;
1100+
10771101
xdrCommandHash = HASH_create_table(37, &xdr_cmd_hash_func,
10781102
&xdr_cmd_cmp_key, &xdr_cmd_key_for_data);
10791103
if (!xdrCommandHash)
@@ -1082,6 +1106,10 @@ void CMD_register_command(struct CMD_XDRCommandInfo *cmd, int override)
10821106
table = xdrCommandHash;
10831107
}
10841108
else if (cmd->types) {
1109+
if (!cleanup_reg)
1110+
atexit(&CMD_hash_cleanup);
1111+
cleanup_reg = 1;
1112+
10851113
node = malloc(sizeof(*node));
10861114
if (!node)
10871115
return;
@@ -1091,6 +1119,7 @@ void CMD_register_command(struct CMD_XDRCommandInfo *cmd, int override)
10911119

10921120
if (cmd->params)
10931121
cmd->parameter = XDR_definition_for_type(cmd->params);
1122+
10941123
return;
10951124
}
10961125
else
@@ -1140,6 +1169,10 @@ void CMD_register_error(struct CMD_ErrorInfo *err)
11401169
return;
11411170

11421171
if (!xdrErrorHash) {
1172+
if (!cleanup_reg)
1173+
atexit(&CMD_hash_cleanup);
1174+
cleanup_reg = 1;
1175+
11431176
xdrErrorHash = HASH_create_table(37, &xdr_err_hash_func,
11441177
&xdr_err_cmp_key, &xdr_err_key_for_data);
11451178
if (!xdrErrorHash)

hashtable.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,16 @@ void *HASH_remove_data(struct HashTable *table, void *data)
133133
void HASH_free_table(struct HashTable *table)
134134
{
135135
int i;
136+
struct HashNode *node;
136137

137138
if (!table)
138139
return;
139140

140141
for(i = 0; i < table->hashSize; i++)
141-
while(table->buckets[i])
142-
HASH_remove_key(table, table->buckets[i]->key);
142+
while((node = table->buckets[i])) {
143+
table->buckets[i] = node->next;
144+
free(node);
145+
}
143146

144147
free(table);
145148
}

xdr.c

+8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ static int xdr_struct_cmp_key(void *key1, void *key2)
3636
return 0;
3737
}
3838

39+
static void XDR_cleanup(void)
40+
{
41+
if (structHash)
42+
HASH_free_table(structHash);
43+
structHash = NULL;
44+
}
45+
3946
void XDR_register_struct(struct XDR_StructDefinition *def)
4047
{
4148
if (!def)
@@ -46,6 +53,7 @@ void XDR_register_struct(struct XDR_StructDefinition *def)
4653
&xdr_struct_cmp_key, &xdr_struct_key_for_data);
4754
if (!structHash)
4855
return;
56+
atexit(&XDR_cleanup);
4957
}
5058

5159
HASH_add_data(structHash, def);

0 commit comments

Comments
 (0)