diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ce60a1b..0b774f83 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -420,6 +420,7 @@ auto_test(encryptsave) auto_test(messenger) auto_test(network) auto_test(onion) +auto_test(resource_leak) auto_test(save_friend) auto_test(skeleton) auto_test(tox) diff --git a/auto_tests/resource_leak_test.c b/auto_tests/resource_leak_test.c new file mode 100644 index 00000000..aab1fcd7 --- /dev/null +++ b/auto_tests/resource_leak_test.c @@ -0,0 +1,50 @@ +#include "helpers.h" + +// See man 2 sbrk. +#if _BSD_SOURCE || _SVID_SOURCE || \ + (_XOPEN_SOURCE >= 500 || \ + _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) && \ + !(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) +#define HAVE_SBRK 1 +#endif + +#if HAVE_SBRK +#include +#include +#endif + +#define ITERATIONS 20000 + +int main(void) +{ + int i; + + puts("Warming up: creating/deleting 10 tox instances"); + + // Warm-up. + for (i = 0; i < 10; i++) { + Tox *tox = tox_new(0, 0); + tox_iterate(tox, NULL); + tox_kill(tox); + } + +#if HAVE_SBRK + // Low water mark. + char *hwm = (char *)sbrk(0); +#endif + printf("Creating/deleting %d tox instances\n", ITERATIONS); + + for (i = 0; i < ITERATIONS; i++) { + Tox *tox = tox_new(0, 0); + tox_iterate(tox, NULL); + tox_kill(tox); +#if HAVE_SBRK + char *next_hwm = (char *)sbrk(0); + assert(hwm == next_hwm); +#endif + } + + puts("Success: no resource leaks detected"); + + return 0; +}