Skip to content

Commit

Permalink
fixing memory leak with destructors examples and documented it's unus…
Browse files Browse the repository at this point in the history
…ual semantics
  • Loading branch information
eteran committed Mar 15, 2024
1 parent 4dbe408 commit 84cd3f8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
14 changes: 12 additions & 2 deletions cvector.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,17 @@
#define cvector_clib_memmove memmove
#endif

typedef void (*cvector_elem_destructor_t)(void *elem);
/* NOTE: Similar to C's qsort and bsearch, you will receive a T*
* for a vector of Ts. This means that you cannot use `free` directly
* as a destructor. Instead if you have for example a cvector_vector_type(int *)
* you will need to supply a function which casts `elem_ptr` to an `int**`
* and then does a free on what that pointer points to:
*
* ex:
*
* void free_int(void *p) { free(*(int **)p); }
*/
typedef void (*cvector_elem_destructor_t)(void *elem_ptr);

typedef struct cvector_metadata_t {
size_t size;
Expand Down Expand Up @@ -367,7 +377,7 @@ typedef struct cvector_metadata_t {

/**
* @brief cvector_set_elem_destructor - set the element destructor function
* used to clean up removed elements
* used to clean up removed elements. The vector must NOT be NULL for this to do anything.
* @param vec - the vector
* @param elem_destructor_fn - function pointer of type cvector_elem_destructor_t used to destroy elements
* @return void
Expand Down
9 changes: 8 additions & 1 deletion test.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@
#include "cvector.h"
#include "cvector_utils.h"

static void free_string(void *str) {
if (str) {
free(*(char **)str);
}
}

int main() {

cvector_vector_type(int) v = NULL;
cvector_vector_type(int) a = NULL;
cvector_vector_type(int) b = NULL;
cvector_vector_type(int) c = NULL;
cvector_vector_type(char *) str_vect = NULL;
cvector_set_elem_destructor(str_vect, free);
cvector_init(str_vect, 1, free_string);

/* add some elements to the back */
cvector_push_back(v, 10);
Expand Down
16 changes: 14 additions & 2 deletions unit-tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
#include <stdarg.h>
#include <stdlib.h>

static void free_string(void *ptr) {
if (ptr) {
free(*(char **)ptr);
}
}

static void free_int(void *ptr) {
if (ptr) {
free(*(int **)ptr);
}
}

UTEST(test, cvector_back) {
cvector_vector_type(int) v = NULL;
cvector_push_back(v, 0);
Expand Down Expand Up @@ -259,7 +271,7 @@ UTEST(test, vector_reserve) {
UTEST(test, vector_free_all) {
int i;
cvector_vector_type(char *) v = NULL;
cvector_set_elem_destructor(v, free);
cvector_init(v, 1, free_string);
for (i = 0; i < 10; ++i) {
char *p = malloc(6);
strcpy(p, "hello");
Expand All @@ -276,7 +288,7 @@ UTEST(test, vector_for_each_int) {
cvector_iterator(int *) it;
int i;
cvector_vector_type(int *) v = NULL;
cvector_set_elem_destructor(v, free);
cvector_init(v, 1, free_int);
for (i = 0; i < 10; ++i) {
int *p = malloc(sizeof(int));
*p = 42;
Expand Down

0 comments on commit 84cd3f8

Please sign in to comment.