I'm working through Zed Shaw's Learn C The Hard Way and had a question regarding exercise 19 (description and link below).
The skinny: while reading/entering the code, it struck me that we never explicitly free the memory which we malloc when creating the various objects (more detail below) at the start of program execution. It's confirmed by valgrind: valgrind reports that 12 blocks (or 608 bytes) are allocated at the start of execution; these blocks are reported as lost (most are indirectly lost and some is directly lost) at program termination.
So...is this code representative of good (or even acceptable) memory management techniques? Or is the lack of memory deallocation a bug? Seems to be the latter but wanted to get others' opinions since I'm a C n00b.
(Given that none of the accompanying exercises involve changing the program to ensure we do free the malloc-ed memory, I assume that the posted code is intended to be sound, i.e., representative of good coding practices...)
I'm including an overview of the exercise below. Rather than re-post the code in full, I'm including the link: http://c.learncodethehardway.org/book/ex19.html.
(Mods: sorry if it isn't kosher to link to external sites, please lmk and I'll take it down, I just didn't want to reproduce the material posted on his site due to potential copyright issues.)
Overview of exercise:
The exercise is to make a simple text-based RPG in which the user can (i) choose to move from room to room (i.e., from any room, you can choose to go north/south/east/west or some subset thereof) and (ii) choose to attack a monster (who is in one of the rooms). A quick summary of the implementation (focusing on the relevant parts) is as follows:
- We define a type(+struct) "Object" (in the object.h and object.c files on the linked page), within which we include (among other functions) a constructor which calls 'malloc' and a destructor (probably not the technical term) which calls 'free'.
- In the ex19.h file, we go on to define the following types(+structs): Map, Room and Monster, each of which includes an "Object" (i.e., what we'd defined in object.h and object.c) as well as some other stuff.
- Gameflow is defined in the ex19.c file. It's in this file that we create instances of a Map, a Monster and a couple of Rooms.
As you can see, we never call a function to deallocate the memory (neither 'free' nor the 'Object_destroy' function which we declare/define in object.h/object.c) for the Map, Monster and Room objects.
Note: the Valgrind output below is the result of code which I copy-pasted from the site; I made sure not to use my retyped versions of the files so as not to introduce errors.
Valgrind output:
==10184==
==10184== HEAP SUMMARY:
==10184== in use at exit: 608 bytes in 12 blocks
==10184== total heap usage: 12 allocs, 0 frees, 608 bytes allocated
==10184==
==10184== 608 (64 direct, 544 indirect) bytes in 1 blocks are definitely lost in loss record 12 of 12
==10184== at 0x4C2C934: calloc (vg_replace_malloc.c:623)
==10184== by 0x400FCF: Object_new (object.c:52)
==10184== by 0x400E53: main (ex19.c:206)
==10184==
==10184== LEAK SUMMARY:
==10184== definitely lost: 64 bytes in 1 blocks
==10184== indirectly lost: 544 bytes in 11 blocks
==10184== possibly lost: 0 bytes in 0 blocks
==10184== still reachable: 0 bytes in 0 blocks
==10184== suppressed: 0 bytes in 0 blocks
==10184==
==10184== For counts of detected and suppressed errors, rerun with: -v
==10184== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)