2

Error summary:

==6520== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
==6520== 
==6520== 1 errors in context 1 of 1:
==6520== Invalid read of size 4
==6520==    at 0x401882: test_START_EMPTY_TREE_TREEBASE_PRINT_FREETREE_TEST (check_test.c:173)
==6520==    by 0x4046B2: srunner_run_all (in /home/travis/build/batousik/Practical-C2/bin/.libs/lt-check_test)
==6520==    by 0x4019EF: main (check_test.c:341)
==6520==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

Setup:

 int_arr_ptr = malloc(arr_size * (sizeof(int)));
 ptr_tree_base_int_1 = new_base(comp_ints, clean_ints, print_ints);
 if(!ptr_tree_base_int_1 || !int_arr_ptr) {
    printf("Error allocating memory in test Setup\n");
    fflush(stdout);
    assert(NULL);
}
for (int i = 0; i < arr_size; ++i) {
    int r = rand() % 20000;
    *(int_arr_ptr+i) = r;
    printf("setup::%d\n",*(int_arr_ptr + i));
    fflush(stdout);
}

Function part:

    int *ptr_helper;
    for (int i = 0; i < arr_size; ++i) {
        ptr_helper = malloc(sizeof(int));
        if (!ptr_helper)
            ck_abort_msg("FAILED memory allocation\n");
        printf("%d\n",*(int_arr_ptr + i));
        fflush(stdout);
        //*ptr_helper =
        isValid = insert(ptr_tree_base_int_1, ptr_helper);
        ck_assert_int_eq(isValid, true);
    }

Teardown:

if (ptr_tree_base_int_1){
    isValid = freeTree(ptr_tree_base_int_1);
    ck_assert_int_eq(isValid, true);
    free(ptr_tree_base_int_1);
    ptr_tree_base_int_1 = NULL;
}
if(int_arr_ptr){
    free(int_arr_ptr);
    int_arr_ptr = NULL;
}

Line 173 is:

printf("%d\n",*(int_arr_ptr + i));
Community
  • 1
  • 1
Bato-Bair Tsyrenov
  • 1,174
  • 3
  • 17
  • 40
  • Does your real code test whether `malloc()` fails here: `int_arr_ptr = malloc(arr_size * (sizeof(int)));`? – alk Mar 08 '15 at 12:24
  • Your code looks fine.. Is all of it in `main`? And if not then can you include the code that calls the setup and the function? – Daniel Kleinstein Mar 08 '15 at 12:25
  • 2
    `int_arr_ptr` is `NULL` in the "Function part" . I'm going to go ahead and guess that you forgot C uses pass-by-value when doing the setup – M.M Mar 08 '15 at 12:25
  • @alk there's a test for that just after the malloc – M.M Mar 08 '15 at 12:25
  • Hu, yes indeed... :-} @MattMcNabb – alk Mar 08 '15 at 12:26
  • Not relevant to your question, but "Error ..." sounds lke an error message. Error messages belong on stderr. `fprintf( stderr, "Error ...")` is more appropriate than `printf( "Error ...")` – William Pursell Mar 08 '15 at 12:28
  • I have no clue how setup and teardown is done, that might be a problem :) http://check.sourceforge.net/ otherwise all the variables are global, – Bato-Bair Tsyrenov Mar 08 '15 at 12:29
  • 2
    @Batousik debug your code and follow the value of `int_arr_ptr` and see where it became NULL. To avoid us having to guess, you would have to follow the code posting guidelines [here](http://stackoverflow.com/help/mcve) – M.M Mar 08 '15 at 12:30
  • @Matt So the code posted up there is 100% fine? the It is something to do with the framework I'm using, How can I debug C code? – Bato-Bair Tsyrenov Mar 08 '15 at 12:33
  • @Batousik use gdb. You only showed fragments of your code so it's impossible for me to say how `int_arr_ptr` came to be NULL in that particular fragment – M.M Mar 08 '15 at 12:55
  • @Matt So I have had commented the rest of the code out and that what exactly I had apart from main that starts the test suite, and ads this one test, I wanted to know if that code is okay, it did end up with things to do with check framework – Bato-Bair Tsyrenov Mar 08 '15 at 13:28

1 Answers1

1

Apparently naming functions

void setup(void);
void teardown(void); 

void setup_x(void);
void teardown_x(void);

different names and adding this line to main()

tcase_add_checked_fixture(tc_core, setup_x, teardown_x);

Solves the problem.

API: http://check.sourceforge.net/doc/doxygen/html/check_8h.html

REFERENCE: http://check.sourceforge.net/doc/check_html/check_4.html#Test-Fixtures

Bato-Bair Tsyrenov
  • 1,174
  • 3
  • 17
  • 40