我遇到了一个问题,当我在结构中有一个std :: vector时,我堆分配该结构,当结构被释放时,向量泄漏。关于如何防止这种情况的任何想法?
以下是代码:
#include <cstdlib>
#include <string>
#include <vector>
struct foo {
std::vector<std::string> bar;
};
const std::vector<std::string> kSample = {"test", "1", "2", "3", "4", "5"};
int main(int argc, char *argv[]) {
struct foo *allocated = new foo;
for (const auto& i : kSample) {
allocated->bar.push_back(i);
}
free(allocated);
return 0;
}
这是运行此代码的valgrind结果:
==18131== 192 bytes in 1 blocks are definitely lost in loss record 51 of 76
==18131== at 0x6DFB: malloc (in /usr/local/Cellar/valgrind/3.9.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==18131== by 0x4728D: operator new(unsigned long) (in /usr/lib/libc++.1.dylib)
==18131== by 0x10000283D: std::__1::__split_buffer<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >&) (in ./build/tools/test)
==18131== by 0x100001EEC: std::__1::__split_buffer<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >&) (in ./build/tools/test)
==18131== by 0x100001D9B: void std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::__push_back_slow_path<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) (in ./build/tools/test)
==18131== by 0x100001278: main (in ./build/tools/test)
==18131==
==18131== LEAK SUMMARY:
==18131== definitely lost: 192 bytes in 1 blocks
==18131== indirectly lost: 0 bytes in 0 blocks
==18131== possibly lost: 0 bytes in 0 blocks
==18131== still reachable: 0 bytes in 0 blocks
==18131== suppressed: 25,768 bytes in 377 blocks
答案 0 :(得分:5)
你应该致电delete allocated;
。 new
和delete
应成对使用。