Skip to content

Commit f4b59b0

Browse files
author
Damien Churchill
committed
fix all the current memory leaks
1 parent 3f54aa0 commit f4b59b0

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

blksync.c

+24-3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ int main(int argc, char **argv) {
5252
pthread_t *workers;
5353
pthread_attr_t pthread_custom_attr;
5454
params *p;
55+
Action action;
5556
Chunk chunk;
5657
mqd_t r_queue, w_queue;
5758
struct mq_attr qattrs;
@@ -105,11 +106,15 @@ int main(int argc, char **argv) {
105106

106107
// Initialize the variables
107108
buffer = (unsigned char *)malloc(chunk_size);
109+
assert(buffer != NULL);
108110

109111
// Initialize the threads
110112
workers = (pthread_t *)malloc(NUM_THREADS * sizeof(workers));
113+
assert(workers != NULL);
114+
111115
pthread_attr_init(&pthread_custom_attr);
112116
p = (params *)malloc(sizeof(params) * NUM_THREADS);
117+
assert(p != NULL);
113118

114119
for (i = 0; i < NUM_THREADS; i++) {
115120
p[i].id = i;
@@ -139,27 +144,38 @@ int main(int argc, char **argv) {
139144

140145
// Now we have a chunk in our buffer, stick it on the queue
141146
chunk = bs_new_chunk(chunk_count, buffer, hash, chunk_size, hash_length);
147+
action = bs_new_action(HASH_CHUNK, chunk);
142148
printf("sender chunk-%d: %p\n", chunk_count, chunk);
143-
if (mq_send(r_queue, (char *)bs_new_action(HASH_CHUNK, chunk), MSG_SIZE, 5) == -1)
149+
if (mq_send(r_queue, (char *)action, MSG_SIZE, 5) == -1)
144150
perror("Unable to send to read queue");
145151

152+
free(action);
153+
146154
chunk_count++;
147155

148-
if (chunk_count >= 4)
156+
if (chunk_count >= 1)
149157
break;
150158
}
151159

152160
// End the threads
153161
for (i = 0; i < NUM_THREADS; i++) {
154-
if (mq_send(r_queue, (char *)bs_new_action(END_THREAD, hash), MSG_SIZE, 5) == -1)
162+
action = bs_new_action(END_THREAD, hash);
163+
if (mq_send(r_queue, (char *)action, MSG_SIZE, 5) == -1)
155164
perror("Unable to send to read queue");
165+
free(action);
156166
}
157167

158168
// Wait for the threads to exit
159169
for (i = 0; i < NUM_THREADS; i++) {
160170
pthread_join(workers[i], NULL);
161171
}
162172

173+
// Tidy up after ourselves
174+
free(p);
175+
free(buffer);
176+
free(workers);
177+
178+
// Close the queues
163179
if (mq_close(r_queue) == -1)
164180
perror("Cannot close read-queue");
165181

@@ -172,5 +188,10 @@ int main(int argc, char **argv) {
172188
if (mq_unlink("/bsync-wqueue") == -1)
173189
perror("Cannot unlink write-queue");
174190

191+
// Close the files
192+
fclose(bd_fp);
193+
fclose(bf_fp);
194+
fclose(h_fp);
195+
175196
return 0;
176197
};

chunk.c

+2
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,7 @@ void bs_destroy_chunk(Chunk chunk) {
5454
memset(chunk->data, 0, chunk->chunk_size);
5555
memset(chunk->hash, 0, chunk->hash_length);
5656
chunk->number = -1;
57+
free(chunk->data);
58+
free(chunk->hash);
5759
free(chunk);
5860
}

testAction.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,24 @@ void test_action_simple(void) {
2929
assert(action->type == HASH_CHUNK);
3030

3131
free(buffer);
32+
free(action);
3233
}
3334

3435
void test_action_loop(void) {
3536
int i;
36-
char *buffer;
37+
char *buffer, *format = "thisisrandomteststringnumber%04d";
3738
Action action;
3839

3940
buffer = malloc(32);
4041

4142
for (i = 0; i < 10000; i++) {
42-
snprintf(buffer, 32, "thisisrandomteststringnumber%04d", i);
43+
snprintf(buffer, 32, format, i);
4344

4445
action = bs_new_action(HASH_CHUNK, buffer);
4546

4647
assert(memcmp(action->data, buffer, 32) == 0);
4748
assert(action->type == HASH_CHUNK);
49+
free(action);
4850
}
4951

5052
free(buffer);

testChunk.c

+2
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ void test_chunk(void) {
3636

3737
assert(memcmp(chunk->data, "test", 4) == 0);
3838
assert(memcmp(chunk->hash, "thistesthashforatest", 20) == 0);
39+
40+
bs_destroy_chunk(chunk);
3941
}

worker.c

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ void *bs_hash_chunk(void *arg) {
6868

6969
bs_print_hash((unsigned char *)hash, p->hash_length);
7070
printf("\n");
71+
72+
bs_destroy_chunk(chunk);
7173
//printf("new_hash: %s\n", chunk->hash);
7274

7375
//if (memcmp(chunk->hash, new_hash, SHA1_LENGTH) != 0) {

0 commit comments

Comments
 (0)