@romajke

На что жалуется Valgrind?

Помогите разобраться, на что жалуется Valgrind?
Функция высвобождает память каждого элемента хэш таблицы. Причем память освобождается корректно, утечек нет.

unload func
bool unload(void)
{
    for (int i=0; i<26; i++)
    {
        node *current=hashtable[i];
        if (current)
        {
            while(current->next)
            {
                free (current->next);
                current=current->next;

            }
            free (hashtable[i]);
        }
    }
    return true;

}

Но в этих строках он выдает четыре ошибки:
while(current->next)
            {
                free (current->next);
                current=current->next;

            }


Valgrind log
==1812== 
==1812== HEAP SUMMARY:
==1812==     in use at exit: 0 bytes in 0 blocks
==1812==   total heap usage: 5 allocs, 5 frees, 1,304 bytes allocated
==1812== 
==1812== All heap blocks were freed -- no leaks are possible
==1812== 
==1812== ERROR SUMMARY: 4 errors from 3 contexts (suppressed: 0 from 0)
==1812== 
==1812== 1 errors in context 1 of 3:
==1812== Invalid read of size 8
==1812==    at 0x4013BC: unload (dictionary.c:114)
==1812==    by 0x400EE1: main (speller.c:152)
==1812==  Address 0x5200370 is 48 bytes inside a block of size 56 free'd
==1812==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1812==    by 0x4013B7: unload (dictionary.c:113)
==1812==    by 0x400EE1: main (speller.c:152)
==1812== 
==1812== 
==1812== 1 errors in context 2 of 3:
==1812== Invalid read of size 8
==1812==    at 0x4013AC: unload (dictionary.c:113)
==1812==    by 0x400EE1: main (speller.c:152)
==1812==  Address 0x5200370 is 48 bytes inside a block of size 56 free'd
==1812==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1812==    by 0x4013B7: unload (dictionary.c:113)
==1812==    by 0x400EE1: main (speller.c:152)
==1812== 
==1812== 
==1812== 2 errors in context 3 of 3:
==1812== Invalid read of size 8
==1812==    at 0x40139D: unload (dictionary.c:111)
==1812==    by 0x400EE1: main (speller.c:152)
==1812==  Address 0x5200370 is 48 bytes inside a block of size 56 free'd
==1812==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1812==    by 0x4013B7: unload (dictionary.c:113)
==1812==    by 0x400EE1: main (speller.c:152)
==1812== 
==1812== ERROR SUMMARY: 4 errors from 3 contexts (suppressed: 0 from 0)
  • Вопрос задан
  • 311 просмотров
Решения вопроса 1
Пригласить эксперта
Ответы на вопрос 1
@DancingOnWater
free (current->next);

Здесь вы убиваете объект
current=current->next;

Здесь вы запоминаете адрес, ведущий убитый объект, т.е. на мусор в памяти.
while(current->next)

А здесь вы его используете.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы