danwritecode / clings

rustlings for C....clings
MIT License
42 stars 7 forks source link

Fixing memory leaks #157

Closed danwritecode closed 4 months ago

danwritecode commented 4 months ago

resolves #155

danwritecode commented 4 months ago

Got memory leaks down to the following:

==3416183== HEAP SUMMARY:
==3416183==     in use at exit: 64 bytes in 2 blocks
==3416183==   total heap usage: 550 allocs, 548 frees, 1,053,184 bytes allocated
==3416183== 
==3416183== 48 bytes in 1 blocks are indirectly lost in loss record 1 of 2
==3416183==    at 0x48487A9: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==3416183==    by 0x10B771: get_exercises (in /home/dan/documents/apps/clings/clings)
==3416183==    by 0x10B607: create_exercise (in /home/dan/documents/apps/clings/clings)
==3416183==    by 0x10960B: main (in /home/dan/documents/apps/clings/clings)
==3416183== 
==3416183== 64 (16 direct, 48 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
==3416183==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==3416183==    by 0x10B6D0: get_exercises (in /home/dan/documents/apps/clings/clings)
==3416183==    by 0x10B607: create_exercise (in /home/dan/documents/apps/clings/clings)
==3416183==    by 0x10960B: main (in /home/dan/documents/apps/clings/clings)
==3416183== 
==3416183== LEAK SUMMARY:
==3416183==    definitely lost: 16 bytes in 1 blocks
==3416183==    indirectly lost: 48 bytes in 1 blocks
==3416183==      possibly lost: 0 bytes in 0 blocks
==3416183==    still reachable: 0 bytes in 0 blocks
==3416183==         suppressed: 0 bytes in 0 blocks
==3416183== 
==3416183== For lists of detected and suppressed errors, rerun with: -s
==3416183== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

I can't for the life of me figure out what is still missing, @7etsuo you mind taking a look here? I've gone over this with a fine tooth comb and just can't see what i'm failing to free.

7etsuo commented 4 months ago

Yeah, i'll look. I have a feeling it's because of the SIGINT. You have a signal handler for ctrl+c. Maybe make a global struct and use a void (dtors)(void); for cleanup. Something like this for example. I wlll look closer at the code later though if it's not this.

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <assert.h>

typedef struct DirStuff {
    void *data;
    void (*dtors)(void*);
} DirStuff;

void cleanup(void *vp) {
    free(vp);
}

DirStuff global_struct;

void handle_sigint(int sig) {
    global_struct.dtors(global_struct.data);
    exit(0);
}

int main() {
    signal(SIGINT, handle_sigint);

    global_struct.data = malloc(100);
    assert(global_struct.data != NULL);

    global_struct.dtors = cleanup;

    printf("Program is running. Press CTRL+C to stop.\n");

    while (1) {
    }

    return 0;
}
danwritecode commented 4 months ago

@sleaper this is merged into master...thank you for squashing those memory leaks!!! Huge help, really appreciated.