【问题标题】:memory leak with allocated struct containing a vector内存泄漏,分配的结构包含向量
【发布时间】:2014-10-23 01:28:46
【问题描述】:

我遇到了一个问题,当我在结构中有一个 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

【问题讨论】:

  • newfree 是两个完全不同的内存分配系统。不要把它们结合起来。所有new/new[] 需要对应的delete/delete[]
  • main 可能只是foo allocated{{std::begin(kSample), std::end(kSample)}};
  • 另外,为什么要使用new 创建结构? C++ 不是 Java——您不需要使用 new 创建对象。另外,为什么要在声明中使用struct 关键字?您在阅读 C 书籍而不是 C++ 书籍吗? foo allocated; 这就是你所需要的。
  • 这是 SQLite API 不同部分之间更大、更钝的交互集的小复制。下面的答案(使用 delete 而不是 new)解决了这个问题(d'oh)。
  • @marpaia 主要的一点是你不应该在不必要的时候使用new。如果必须,请使用智能指针和 RAII,以便动态创建的对象自动释放。

标签: c++ vector memory-leaks struct


【解决方案1】:

您应该致电delete allocated;newdelete 应该成对使用。

【讨论】:

    猜你喜欢
    • 2014-02-18
    • 2011-03-20
    • 2016-09-12
    • 1970-01-01
    • 2016-03-28
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多