我的老师告诉我我有内存问题,我认为这是析构函数和复制构造函数的错误,但我找不到问题。下面是我的部分代码和valgrind的报告。
struct Board
{
string s;
Board* next;
Board* prev;
};
typedef Board* Pboard;
struct Player::Impl
{
Pboard head;
Pboard tail;
int player;
void destroy(Pboard head);
};
void Player::Impl::destroy(Pboard head)
{
if (head) {
destroy(head->next);
delete head;
}
}
Player::Player(int player_nr)
{
if (player_nr != 1 && player_nr != 2) throw player_exception{ (player_exception::err_type::index_out_of_bounds), "Giocatore non definito"};
pimpl = new Impl;
pimpl->player = player_nr;
pimpl->head = nullptr;
pimpl->tail = nullptr;
}
Player::~Player()
{
pimpl->destroy(pimpl->head);
delete pimpl;
}
Player::Player(const Player& source)
{
pimpl = new Impl;
pimpl = source.pimpl;
}
Player& Player::operator=(const Player& s)
{
if (s.pimpl->head == nullptr) {
pimpl->head = nullptr;
pimpl->tail = nullptr;
}
else {
pimpl->destroy(pimpl->head);
pimpl->head = nullptr;
pimpl->tail = nullptr;
Pboard pb = s.pimpl->head;
pimpl->player = s.pimpl->player;
while (pb) {
Pboard pboard = new Board;
pboard->next = nullptr;
pboard->s = pb->s;
pboard->prev = pimpl->tail;
if (pimpl->tail != nullptr)
pimpl->tail->next = pboard;
else
pimpl->head = pboard;
pimpl->tail = pboard;
pb = pb->next;
}
}
return *this;
}```
我尝试了各种解决方案,但我真的不知道它是否有效,因为他不让我看他的主,我不得不用我自己的。这是他发给我的valgrind的报告。
==235== Memcheck, a memory error detector
==235== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==235== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==235== Command: ../consegne/prova_decompressed/890361/benchmark /home/nico/progetti_PEL/scripts/testcase
==235==
==235== Invalid read of size 8
==235== at 0x1178E0: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDC2: check_copy_constr(double) (benchmark.cpp:132)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Address 0x4dc8a20 is 0 bytes inside a block of size 24 free'd
==235== at 0x483D1CF: operator delete(void*, unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x117906: Player::~Player() (player_filt.cpp:39)
==235== by 0x10CDB3: check_copy_constr(double) (benchmark.cpp:137)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Block was alloc'd at
==235== at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x117847: Player::Player(int) (player_filt.cpp:30)
==235== by 0x10CB39: check_copy_constr(double) (benchmark.cpp:132)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235==
==235== Invalid read of size 8
==235== at 0x117778: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDC2: check_copy_constr(double) (benchmark.cpp:132)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Address 0x4dcae10 is 32 bytes inside a block of size 48 free'd
==235== at 0x483D1CF: operator delete(void*, unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x1177A8: Player::Impl::destroy(Board*) (player_filt.cpp:23)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDB3: check_copy_constr(double) (benchmark.cpp:137)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Block was alloc'd at
==235== at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x117FE3: Player::load_board(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (player_filt.cpp:119)
==235== by 0x10CB69: check_copy_constr(double) (benchmark.cpp:133)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235==
==235== Invalid read of size 8
==235== at 0x117778: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDC2: check_copy_constr(double) (benchmark.cpp:132)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Address 0x4dcd270 is 32 bytes inside a block of size 48 free'd
==235== at 0x483D1CF: operator delete(void*, unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x1177A8: Player::Impl::destroy(Board*) (player_filt.cpp:23)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDB3: check_copy_constr(double) (benchmark.cpp:137)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Block was alloc'd at
==235== at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x117FE3: Player::load_board(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (player_filt.cpp:119)
==235== by 0x10CBA5: check_copy_constr(double) (benchmark.cpp:134)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235==
==235== Invalid read of size 8
==235== at 0x117778: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDC2: check_copy_constr(double) (benchmark.cpp:132)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Address 0x4dcf6d0 is 32 bytes inside a block of size 48 free'd
==235== at 0x483D1CF: operator delete(void*, unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x1177A8: Player::Impl::destroy(Board*) (player_filt.cpp:23)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDB3: check_copy_constr(double) (benchmark.cpp:137)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Block was alloc'd at
==235== at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x117FE3: Player::load_board(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (player_filt.cpp:119)
==235== by 0x10CBE1: check_copy_constr(double) (benchmark.cpp:135)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235==
==235== Invalid read of size 8
==235== at 0x499D5C4: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==235== by 0x11B7DB: Board::~Board() (player_filt.cpp:3)
==235== by 0x11779B: Player::Impl::destroy(Board*) (player_filt.cpp:23)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDC2: check_copy_constr(double) (benchmark.cpp:132)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Address 0x4dcf6b0 is 0 bytes inside a block of size 48 free'd
==235== at 0x483D1CF: operator delete(void*, unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x1177A8: Player::Impl::destroy(Board*) (player_filt.cpp:23)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDB3: check_copy_constr(double) (benchmark.cpp:137)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Block was alloc'd at
==235== at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x117FE3: Player::load_board(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (player_filt.cpp:119)
==235== by 0x10CBE1: check_copy_constr(double) (benchmark.cpp:135)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235==
==235== Invalid free() / delete / delete[] / realloc()
==235== at 0x483CFBF: operator delete(void*) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x11B7DB: Board::~Board() (player_filt.cpp:3)
==235== by 0x11779B: Player::Impl::destroy(Board*) (player_filt.cpp:23)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDC2: check_copy_constr(double) (benchmark.cpp:132)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Address 0x4dcf720 is 0 bytes inside a block of size 51 free'd
==235== at 0x483CFBF: operator delete(void*) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x11B7DB: Board::~Board() (player_filt.cpp:3)
==235== by 0x11779B: Player::Impl::destroy(Board*) (player_filt.cpp:23)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDB3: check_copy_constr(double) (benchmark.cpp:137)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Block was alloc'd at
==235== at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x499D29E: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==235== by 0x499D5FD: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==235== by 0x11801D: Player::load_board(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (player_filt.cpp:121)
==235== by 0x10CBE1: check_copy_constr(double) (benchmark.cpp:135)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235==
==235== Invalid free() / delete / delete[] / realloc()
==235== at 0x483D1CF: operator delete(void*, unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x1177A8: Player::Impl::destroy(Board*) (player_filt.cpp:23)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDC2: check_copy_constr(double) (benchmark.cpp:132)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Address 0x4dcf6b0 is 0 bytes inside a block of size 48 free'd
==235== at 0x483D1CF: operator delete(void*, unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x1177A8: Player::Impl::destroy(Board*) (player_filt.cpp:23)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDB3: check_copy_constr(double) (benchmark.cpp:137)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Block was alloc'd at
==235== at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x117FE3: Player::load_board(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (player_filt.cpp:119)
==235== by 0x10CBE1: check_copy_constr(double) (benchmark.cpp:135)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235==
==235== Invalid free() / delete / delete[] / realloc()
==235== at 0x483D1CF: operator delete(void*, unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x1177A8: Player::Impl::destroy(Board*) (player_filt.cpp:23)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDC2: check_copy_constr(double) (benchmark.cpp:132)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Address 0x4dcd250 is 0 bytes inside a block of size 48 free'd
==235== at 0x483D1CF: operator delete(void*, unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x1177A8: Player::Impl::destroy(Board*) (player_filt.cpp:23)
==235== by 0x11778A: Player::Impl::destroy(Board*) (player_filt.cpp:22)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDB3: check_copy_constr(double) (benchmark.cpp:137)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Block was alloc'd at
==235== at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x117FE3: Player::load_board(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (player_filt.cpp:119)
==235== by 0x10CBA5: check_copy_constr(double) (benchmark.cpp:134)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235==
==235== Invalid read of size 8
==235== at 0x499D5C4: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==235== by 0x11B7DB: Board::~Board() (player_filt.cpp:3)
==235== by 0x11779B: Player::Impl::destroy(Board*) (player_filt.cpp:23)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDC2: check_copy_constr(double) (benchmark.cpp:132)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Address 0x4dcadf0 is 0 bytes inside a block of size 48 free'd
==235== at 0x483D1CF: operator delete(void*, unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x1177A8: Player::Impl::destroy(Board*) (player_filt.cpp:23)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDB3: check_copy_constr(double) (benchmark.cpp:137)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Block was alloc'd at
==235== at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x117FE3: Player::load_board(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (player_filt.cpp:119)
==235== by 0x10CB69: check_copy_constr(double) (benchmark.cpp:133)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235==
==235== Invalid free() / delete / delete[] / realloc()
==235== at 0x483CFBF: operator delete(void*) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x11B7DB: Board::~Board() (player_filt.cpp:3)
==235== by 0x11779B: Player::Impl::destroy(Board*) (player_filt.cpp:23)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDC2: check_copy_constr(double) (benchmark.cpp:132)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Address 0x4dcae60 is 0 bytes inside a block of size 54 free'd
==235== at 0x483CFBF: operator delete(void*) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x11B7DB: Board::~Board() (player_filt.cpp:3)
==235== by 0x11779B: Player::Impl::destroy(Board*) (player_filt.cpp:23)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDB3: check_copy_constr(double) (benchmark.cpp:137)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Block was alloc'd at
==235== at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x499D29E: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==235== by 0x499D5FD: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==235== by 0x11801D: Player::load_board(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (player_filt.cpp:121)
==235== by 0x10CB69: check_copy_constr(double) (benchmark.cpp:133)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235==
==235== Invalid free() / delete / delete[] / realloc()
==235== at 0x483D1CF: operator delete(void*, unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x1177A8: Player::Impl::destroy(Board*) (player_filt.cpp:23)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDC2: check_copy_constr(double) (benchmark.cpp:132)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Address 0x4dcadf0 is 0 bytes inside a block of size 48 free'd
==235== at 0x483D1CF: operator delete(void*, unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x1177A8: Player::Impl::destroy(Board*) (player_filt.cpp:23)
==235== by 0x1178ED: Player::~Player() (player_filt.cpp:38)
==235== by 0x10CDB3: check_copy_constr(double) (benchmark.cpp:137)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Block was alloc'd at
==235== at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x117FE3: Player::load_board(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (player_filt.cpp:119)
==235== by 0x10CB69: check_copy_constr(double) (benchmark.cpp:133)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235==
==235== Invalid free() / delete / delete[] / realloc()
==235== at 0x483D1CF: operator delete(void*, unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x117906: Player::~Player() (player_filt.cpp:39)
==235== by 0x10CDC2: check_copy_constr(double) (benchmark.cpp:132)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Address 0x4dc8a20 is 0 bytes inside a block of size 24 free'd
==235== at 0x483D1CF: operator delete(void*, unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x117906: Player::~Player() (player_filt.cpp:39)
==235== by 0x10CDB3: check_copy_constr(double) (benchmark.cpp:137)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235== Block was alloc'd at
==235== at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x117847: Player::Player(int) (player_filt.cpp:30)
==235== by 0x10CB39: check_copy_constr(double) (benchmark.cpp:132)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235==
==235== Invalid read of size 8
==235== at 0x499FC08: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==235== by 0x117C0D: Player::operator()(int, int, int) const (player_filt.cpp:91)
==235== by 0x110A10: check_access2(double) (benchmark.cpp:504)
==235== by 0x116916: main (benchmark.cpp:1004)
==235== Address 0x8 is not stack'd, malloc'd or (recently) free'd
==235==
==235==
==235== Process terminating with default action of signal 11 (SIGSEGV)
==235== Access not within mapped region at address 0x8
==235== at 0x499FC08: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==235== by 0x117C0D: Player::operator()(int, int, int) const (player_filt.cpp:91)
==235== by 0x110A10: check_access2(double) (benchmark.cpp:504)
==235== by 0x116916: main (benchmark.cpp:1004)
==235== If you believe this happened as a result of a stack
==235== overflow in your program's main thread (unlikely but
==235== possible), you can try to increase the size of the
==235== main thread stack using the --main-stacksize= flag.
==235== The main thread stack size used in this run was 8388608.
==235==
==235== HEAP SUMMARY:
==235== in use at exit: 188 bytes in 5 blocks
==235== total heap usage: 2,470 allocs, 2,472 frees, 6,973,158 bytes allocated
==235==
==235== 24 bytes in 1 blocks are definitely lost in loss record 2 of 5
==235== at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==235== by 0x117927: Player::Player(Player const&) (player_filt.cpp:44)
==235== by 0x10CC06: check_copy_constr(double) (benchmark.cpp:137)
==235== by 0x1168B9: main (benchmark.cpp:1001)
==235==
==235== LEAK SUMMARY:
==235== definitely lost: 24 bytes in 1 blocks
==235== indirectly lost: 0 bytes in 0 blocks
==235== possibly lost: 0 bytes in 0 blocks
==235== still reachable: 164 bytes in 4 blocks
==235== suppressed: 0 bytes in 0 blocks
==235== Reachable blocks (those to which a pointer was found) are not shown.
==235== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==235==
==235== For lists of detected and suppressed errors, rerun with: -s
==235== ERROR SUMMARY: 16 errors from 14 contexts (suppressed: 0 from 0)
Segmentation fault
STDERR.txt
Visualizzazione di STDERR.txt.
Player的复制构造函数是错误的。您创建了一个新的指针,将其赋值给变量,然后立即告诉该变量指向其他地方。这是内存泄漏……当其中一个玩家被删除时,这会导致UB。
Player::Player(const Player& source)
{
pimpl = new Impl; // dynamical allocation, assign to variable
pimpl = source.pimpl; // now changing where the pointer points to. Memory leak.
// Both players also now point to the same memory location, so the destructor really causes issues.
}