iqbal-lab / Mykrobe-predictor

Antibiotic resistance predictions in minutes on a laptop
Other
50 stars 19 forks source link

Memory leaks in test code #84

Closed iqbal-lab closed 9 years ago

iqbal-lab commented 9 years ago

Found while running valgrind

 CUnit - A Unit testing framework for C - Version 2.1-0
 http://cunit.sourceforge.net/

Suite: Test main functionality of Mykrobe predictor Test: Test gene presence models ... passed Test: Test gene presence minor models ... passed Test: Test gene presence unsure models ... 1 passed Test: Test gene presence unsure models 2 ... 2 passed Test: Test gene presence high CN models ... passed Test: Test gene not present models ... passed Test: Test gene low coverage ... passed Test: Test gene low coverage ont ... passed Test: Test mutation not present models ... S passed Test: Test mutation R models ... R passed Test: Test mutation minor models ... r passed Test: Test mutation custom case ... conf 28.631425 R passed Test: Test mutation test_low_coverage_ont_mut case ... conf 24.974789 R passed

--Run Summary: Type Total Ran Passed Failed suites 1 1 n/a 0 tests 13 13 13 0 asserts 24 24 24 0

==60191== 16 bytes in 1 blocks are definitely lost in loss record 6 of 59 ==60191== at 0x4A07D70: calloc (vg_replace_malloc.c:566) ==60191== by 0x41BC8A: alloc_allele_info (genotyping_known.c:18) ==60191== by 0x402CBB: test_mutation_S (test_mut_models.c:70) ==60191== by 0x4C143C2: run_single_test (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C14195: run_single_suite (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C13976: CU_run_all_tests (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C159E9: basic_run_all_tests (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C15791: CU_basic_run_tests (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x402BFC: main (run_predictor_tests.c:165)

==60191== 16 bytes in 1 blocks are definitely lost in loss record 7 of 59 ==60191== at 0x4A07D70: calloc (vg_replace_malloc.c:566) ==60191== by 0x41BC8A: alloc_allele_info (genotyping_known.c:18) ==60191== by 0x402CF1: test_mutation_S (test_mut_models.c:76) ==60191== by 0x4C143C2: run_single_test (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C14195: run_single_suite (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C13976: CU_run_all_tests (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C159E9: basic_run_all_tests (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C15791: CU_basic_run_tests (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x402BFC: main (run_predictor_tests.c:165)

... others similar...

==60191== 16 bytes in 1 blocks are definitely lost in loss record 9 of 59 ==60191== at 0x4A07D70: calloc (vg_replace_malloc.c:566) ==60191== by 0x41BC8A: alloc_allele_info (genotyping_known.c:18) ==60191== by 0x402F69: test_mutation_R (test_mut_models.c:148) ==60191== by 0x4C143C2: run_single_test (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C14195: run_single_suite (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C13976: CU_run_all_tests (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C159E9: basic_run_all_tests (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C15791: CU_basic_run_tests (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x402BFC: main (run_predictor_tests.c:165) ==60191==

and then some bigger chunks

==60191== 320 (40 direct, 280 indirect) bytes in 1 blocks are definitely lost in loss record 32 of 59 ==60191== at 0x4A07D70: calloc (vg_replace_malloc.c:566) ==60191== by 0x41EF25: alloc_and_init_gene_info (gene_presence.c:484) ==60191== by 0x403961: test_resistotype_gene (test_gene_presence_models.c:19) ==60191== by 0x4C143C2: run_single_test (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C14195: run_single_suite (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C13976: CU_run_all_tests (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C159E9: basic_run_all_tests (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C15791: CU_basic_run_tests (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x402BFC: main (run_predictor_tests.c:165)

... and then even bigger ones

==60191== 1,000 bytes in 1 blocks are definitely lost in loss record 45 of 59 ==60191== at 0x4A07D70: calloc (vg_replace_malloc.c:566) ==60191== by 0x41C594: alloc_and_init_var_on_background (genotyping_known.c:264) ==60191== by 0x41D0FE: alloc_var (genotyping_known.c:686) ==60191== by 0x402D8C: test_mutation_S (test_mut_models.c:90) ==60191== by 0x4C143C2: run_single_test (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C14195: run_single_suite (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C13976: CU_run_all_tests (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C159E9: basic_run_all_tests (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C15791: CU_basic_run_tests (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x402BFC: main (run_predictor_tests.c:165) ==60191==

and finally

==60191== 1,024 (8 direct, 1,016 indirect) bytes in 1 blocks are definitely lost in loss record 56 of 59 ==60191== at 0x4A09320: malloc (vg_replace_malloc.c:263) ==60191== by 0x403029: test_mutation_R (test_mut_models.c:166) ==60191== by 0x4C143C2: run_single_test (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C14195: run_single_suite (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C13976: CU_run_all_tests (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C159E9: basic_run_all_tests (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x4C15791: CU_basic_run_tests (in /Net/fs1/home/zam/bin/lib/libcunit.so.1.0.1) ==60191== by 0x402BFC: main (run_predictor_tests.c:165)

These all look very easy to fix - just free at the end of the test

iqbal-lab commented 9 years ago

I'm fixing.

iqbal-lab commented 9 years ago

Fixed in this commit. This bug was partially due to some places where free's were missing but also you were orphaning code. Basically, you were doing A* zam = alloc(whatever) B* bob = alloc(whatever) zam=bob this loses the memory originally allocated to zam - I fixed by using a copy operator to copy contents

Fixed in da50bb8603ed01e050506f233661813e0b697752