Я экспериментирую с библиотекой поддержки директив.
Этот код работает отлично, и valgrind не показывает проблем с памятью
#include <gsl>
#include <iostream>
int main() {
const int length = 10;
int *arr = new int [length];
auto _ = gsl::finally([arr] { delete[] arr; });
for (int i = 0; i<length; i++){
arr[i] = i;
}
for (int i = 0; i<length; i++){
std::cout << arr[i] << " ";
}
}
Если назначение finally
заявление к auto _
удаляется, valgrind показывает следующие недопустимые операции чтения / записи. Почему мне нужно присвоить оператор finally значению, которое впоследствии больше не требуется?
Я использую gsl-lite, версия 0.28.0.
Сломанный код:
#include <gsl>
#include <iostream>
int main() {
const int length = 10;
int *arr = new int [length];
gsl::finally([arr] { delete[] arr; });
for (int i = 0; i<length; i++){
arr[i] = i;
}
for (int i = 0; i<length; i++){
std::cout << arr[i] << " ";
}
}
Выход Valgrind:
==15802== Memcheck, a memory error detector
==15802== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==15802== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==15802== Command: ./a.out
==15802==
==15802== Invalid write of size 8
==15802== at 0x400E1A: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Address 0x4c3e040 is 0 bytes inside a block of size 40 free'd
==15802== at 0x4A0862D: operator delete[](void*) (vg_replace_malloc.c:621)
==15802== by 0x400E04: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Block was alloc'd at
==15802== at 0x4A07898: operator new[](unsigned long) (vg_replace_malloc.c:423)
==15802== by 0x400DF4: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802==
==15802== Invalid write of size 8
==15802== at 0x400E24: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Address 0x4c3e050 is 16 bytes inside a block of size 40 free'd
==15802== at 0x4A0862D: operator delete[](void*) (vg_replace_malloc.c:621)
==15802== by 0x400E04: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Block was alloc'd at
==15802== at 0x4A07898: operator new[](unsigned long) (vg_replace_malloc.c:423)
==15802== by 0x400DF4: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802==
==15802== Invalid write of size 4
==15802== at 0x400E2B: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Address 0x4c3e060 is 32 bytes inside a block of size 40 free'd
==15802== at 0x4A0862D: operator delete[](void*) (vg_replace_malloc.c:621)
==15802== by 0x400E04: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Block was alloc'd at
==15802== at 0x4A07898: operator new[](unsigned long) (vg_replace_malloc.c:423)
==15802== by 0x400DF4: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802==
==15802== Invalid write of size 4
==15802== at 0x400E34: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Address 0x4c3e064 is 36 bytes inside a block of size 40 free'd
==15802== at 0x4A0862D: operator delete[](void*) (vg_replace_malloc.c:621)
==15802== by 0x400E04: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Block was alloc'd at
==15802== at 0x4A07898: operator new[](unsigned long) (vg_replace_malloc.c:423)
==15802== by 0x400DF4: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802==
==15802== Invalid read of size 4
==15802== at 0x400E45: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Address 0x4c3e040 is 0 bytes inside a block of size 40 free'd
==15802== at 0x4A0862D: operator delete[](void*) (vg_replace_malloc.c:621)
==15802== by 0x400E04: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Block was alloc'd at
==15802== at 0x4A07898: operator new[](unsigned long) (vg_replace_malloc.c:423)
==15802== by 0x400DF4: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802==
0 1 2 3 4 5 6 7 8 9 ==15802==
==15802== HEAP SUMMARY:
==15802== in use at exit: 0 bytes in 0 blocks
==15802== total heap usage: 1 allocs, 1 frees, 40 bytes allocated
==15802==
==15802== All heap blocks were freed -- no leaks are possible
==15802==
==15802== For counts of detected and suppressed errors, rerun with: -v
==15802== ERROR SUMMARY: 16 errors from 5 contexts (suppressed: 4 from 4)
Просто гадать здесь, но кажется, что когда объект, который gsl::finally
возврат разрушается, он вызывает лямбду, которая освобождает память.
В первом случае этот возвращаемый объект присваивается переменной _
который разрушен один раз main
возвращается. Во втором случае объект разрушается сразу после вызова gsl::finally
, что приводит к тому, что любой доступ к памяти впоследствии становится недействительным.
Других решений пока нет …