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
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */19 */
20 20
21#include "error.h"
21#include "log.h"22#include "log.h"
22#include "mem.h"23#include "mem.h"
23#include "tree.h"24#include "tree.h"
@@ -211,32 +212,50 @@ static int cmp(void *a, const void *b) · 187 unmodified lines
211int main(void)212int main(void)
212{213{
213 int i;214 int i;
214 void *k;
215 AVTreeNode *root = NULL, *node = NULL;215 AVTreeNode *root = NULL, *node = NULL;
216 AVLFG prng;216 AVLFG prng;
217 217
218 av_lfg_init(&prng, 1);218 av_lfg_init(&prng, 1);
219 219
220 for (i = 0; i < 10000; i++) {220 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
222 if (check(root) > 999) {230 if (check(root) > 999) {
223 av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i);231 av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i);
224 print(root, 0);232 print(root, 0);
225 return -1;233 return -1;
226 }234 }
235
227 if (!node)236 if (!node)
228 node = av_tree_node_alloc();237 node = av_tree_node_alloc();
229 av_tree_insert(&root, (void *)(j + 1), cmp, &node);238 if (!node) {
230 239 av_log(NULL, AV_LOG_ERROR, "Memory allocation failure.\n");
231 j = av_lfg_get(&prng) % 86294;240 return AVERROR(ENOMEM);
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 }241 }
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);
239 }255 }
256
257 av_tree_destroy(root);
258
240 return 0;259 return 0;
241}260}
242#endif261#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