History

tree-test: Refactor and plug memory leaks

With the most annoying memory leak found by Derek Buitenhuis.

Diego Biurrun committed 13 years ago

Commit c2e936d1 parent a9b04b21 file · +30 −11Browse files

Unified · Split

libavutil/tree.c+30−11
@@ -18,6 +18,7 @@ · 17 unmodified lines
1818 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1919 */
2020
21+#include "error.h"
2122 #include "log.h"
2223 #include "mem.h"
2324 #include "tree.h"
@@ -211,32 +212,50 @@ static int cmp(void *a, const void *b) · 187 unmodified lines
211212 int main(void)
212213 {
213214 int i;
214− void *k;
215215 AVTreeNode *root = NULL, *node = NULL;
216216 AVLFG prng;
217217
218218 av_lfg_init(&prng, 1);
219219
220220 for (i = 0; i < 10000; i++) {
221− int j = av_lfg_get(&prng) % 86294;
221+ AVTreeNode *node2 = NULL;
222+ intptr_t j = av_lfg_get(&prng) % 86294;
223+ void *ret, *jj = (void *)(j + 1);
224+
225+ while (ret = av_tree_find(root, jj, cmp, NULL)) {
226+ j = av_lfg_get(&prng) % 86294;
227+ jj = (void *)(j + 1);
228+ }
229+
222230 if (check(root) > 999) {
223231 av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i);
224232 print(root, 0);
225233 return -1;
226234 }
235+
227236 if (!node)
228237 node = av_tree_node_alloc();
229− av_tree_insert(&root, (void *)(j + 1), cmp, &node);
230−
231− j = av_lfg_get(&prng) % 86294;
232− {
233− AVTreeNode *node2 = NULL;
234− av_tree_insert(&root, (void *)(j + 1), cmp, &node2);
235− k = av_tree_find(root, (void *)(j + 1), cmp, NULL);
236− if (k)
237− av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i);
238+ if (!node) {
239+ av_log(NULL, AV_LOG_ERROR, "Memory allocation failure.\n");
240+ return AVERROR(ENOMEM);
238241 }
242+ av_tree_insert(&root, jj, cmp, &node);
243+
244+ while (ret = av_tree_find(root, jj, cmp, NULL)) {
245+ j = av_lfg_get(&prng) % 86294;
246+ jj = (void *)(j + 1);
247+ }
248+
249+ ret = av_tree_insert(&root, jj, cmp, &node2);
250+ if (ret != jj)
251+ av_tree_destroy(node2);
252+ ret = av_tree_find(root, jj, cmp, NULL);
253+ if (ret)
254+ av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i);
239255 }
256+
257+ av_tree_destroy(root);
258+
240259 return 0;
241260 }
242261 #endif

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t