tree-test: Refactor and plug memory leaks

With the most annoying memory leak found by Derek Buitenhuis.
pull/37/head
Diego Biurrun 11 years ago
parent a9b04b2c43
commit c2e936de07
  1. 41
      libavutil/tree.c

@ -18,6 +18,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include "error.h"
#include "log.h" #include "log.h"
#include "mem.h" #include "mem.h"
#include "tree.h" #include "tree.h"
@ -211,32 +212,50 @@ static int cmp(void *a, const void *b)
int main(void) int main(void)
{ {
int i; int i;
void *k;
AVTreeNode *root = NULL, *node = NULL; AVTreeNode *root = NULL, *node = NULL;
AVLFG prng; AVLFG prng;
av_lfg_init(&prng, 1); av_lfg_init(&prng, 1);
for (i = 0; i < 10000; i++) { for (i = 0; i < 10000; i++) {
int j = av_lfg_get(&prng) % 86294; AVTreeNode *node2 = NULL;
intptr_t j = av_lfg_get(&prng) % 86294;
void *ret, *jj = (void *)(j + 1);
while (ret = av_tree_find(root, jj, cmp, NULL)) {
j = av_lfg_get(&prng) % 86294;
jj = (void *)(j + 1);
}
if (check(root) > 999) { if (check(root) > 999) {
av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i); av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i);
print(root, 0); print(root, 0);
return -1; return -1;
} }
if (!node) if (!node)
node = av_tree_node_alloc(); node = av_tree_node_alloc();
av_tree_insert(&root, (void *)(j + 1), cmp, &node); if (!node) {
av_log(NULL, AV_LOG_ERROR, "Memory allocation failure.\n");
j = av_lfg_get(&prng) % 86294; return AVERROR(ENOMEM);
{
AVTreeNode *node2 = NULL;
av_tree_insert(&root, (void *)(j + 1), cmp, &node2);
k = av_tree_find(root, (void *)(j + 1), cmp, NULL);
if (k)
av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i);
} }
av_tree_insert(&root, jj, cmp, &node);
while (ret = av_tree_find(root, jj, cmp, NULL)) {
j = av_lfg_get(&prng) % 86294;
jj = (void *)(j + 1);
}
ret = av_tree_insert(&root, jj, cmp, &node2);
if (ret != jj)
av_tree_destroy(node2);
ret = av_tree_find(root, jj, cmp, NULL);
if (ret)
av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i);
} }
av_tree_destroy(root);
return 0; return 0;
} }
#endif #endif

Loading…
Cancel
Save